1

我正在尝试结合一些查询,但似乎无法确定。我想知道是否有人可以指出我正确的方向。

以下是声明:

SELECT
I.id,
I.custname,
I.custemail,
I.sku,
DATE_FORMAT(FROM_UNIXTIME(I.ts), '%l:%i:%s %p, %c/%e/%Y') AS ts
FROM images I
WHERE I.stat = 0

SELECT
COUNT(*) AS total1
FROM images
WHERE stat = 1 AND sku = ?

SELECT
COUNT(*) AS total2
FROM images
WHERE stat = 1 
AND sku IN (SELECT subsku FROM combo WHERE sku = ?)

现在我正在使用 3 个单独的查询,并使用代码来添加两个总数并显示它们。但是我现在需要能够按总数的总和进行排序,所以我想将所有这些数据放入一个语句中......就像:

SELECT
I.id,
I.custname,
I.custemail,
I.sku,
DATE_FORMAT(FROM_UNIXTIME(I.ts), '%l:%i:%s %p, %c/%e/%Y') AS ts,
SUM(total1+total2)
FROM images I
WHERE I.stat = 0

但我不确定该怎么做。我尝试了下面的代码,但失败了:

SELECT 
I.id, 
I.custname, 
I.custemail, 
I.sku, 
DATE_FORMAT(FROM_UNIXTIME(I.ts),'%l:%i:%s %p, %c/%e/%Y') AS ts, 
(
    SELECT COUNT(*) AS total1 FROM images WHERE stat = 1 AND (
        sku IN (
            SELECT subsku FROM combo WHERE sku = I.sku
    ) OR sku = I.sku)
) AS skuct 
FROM images I 
WHERE stat = 0

任何帮助将不胜感激。非常感谢!

更新

首先感谢所有提供帮助的人。我一直在处理查询并认为我越来越近了,但我现在遇到了“子查询返回超过 1 行”错误:

SELECT 
I.id, 
I.custname, 
I.custemail, 
I.sku,
DATE_FORMAT(FROM_UNIXTIME(I.ts), '%l:%i:%s %p, %c/%e/%Y') AS ts,
(
    SELECT COUNT(*)
    FROM images
    WHERE stat = 1 
    AND sku = I.sku
    OR sku IN(
        SELECT subsku FROM combo WHERE sku = I.sku
    )
GROUP BY sku
) AS total
FROM images I
WHERE stat = 0

问题是子查询SELECT subsku FROM combo WHERE...返回结果集(0+ 行)与标量。如果我能弄清楚那部分,我认为这会奏效。

4

3 回答 3

2
Select I.id, I.custname, I.custemail, I.sku
    , DATE_FORMAT(FROM_UNIXTIME(I.ts), '%l:%i:%s %p, %c/%e/%Y') AS ts
    ,   (
        Select Sum( Case
                    When I1.sku = ? Then 1
                    When I1.sku In( Select subsku From combo As S1 Where S1.sku = ? ) Then 1
                    Else 0
                    End ) As Total
        From images As I1
        Where I1.stat = 1
        ) As Total
From images As I
Where stat = 0

另一种解决方案

Select I.id, I.custname, I.custemail, I.sku
    , DATE_FORMAT(FROM_UNIXTIME(I.ts), '%l:%i:%s %p, %c/%e/%Y') AS ts
    ,   (
        Select Count(*)
        From images As I1
        Where I1.stat = 1
            And (
                I1.sku = ?
                Or I1.sku In ( Select subsku From combo As S1 Where S1.sku = ? )
                )
        ) As Total
From images As I
Where stat = 0

添加

另一种可能的解决方案:

Select I.id, I.custname, I.custemail, I.sku
    , DATE_FORMAT(FROM_UNIXTIME(I.ts), '%l:%i:%s %p, %c/%e/%Y') AS ts
    ,   (
        Select Sum( Cnt )
        From    (
                Select Count(*) As Cnt
                From images As I1
                    Left Join combo As C1
                        On C1.sku = I1.sku
                Where I1.stat = 1
                    And I1.sku = ?
                    And C1.PrimaryKeyCol Is Null
                Union All 
                Select Count( Distinct I1.PrimaryKeyCol )
                From images As I1
                    Join combo As C1
                        On C1.sku = I1.sku
                Where I1.stat = 1
                    And I1.sku = ?
                ) As Z
        ) As Total
From images As I
Where stat = 0

编辑

如果您正在查找将计数与外部表 sku 列相关联的图像images计数,那就完全不同了。为此,我将使用派生表:

Select I.id, I.custname, I.custemail, I.sku
    , DATE_FORMAT(FROM_UNIXTIME(I.ts), '%l:%i:%s %p, %c/%e/%Y') AS ts
    , Counts.Total
From images As I
    Join    (
            Select Z.sku, Sum(Z.Cnt) As Total
            From    (
                    Select I1.sku, Count(*) As Cnt
                    From images As I1
                        Left Join combo As C1
                            On C1.sku = I1.sku
                    Where I1.stat = 1
                        And C1.PrimaryKeyCol Is Null
                    Group By I1.sku
                    Union All 
                    Select I1.sku, Count( Distinct I1.PrimaryKeyCol )
                    From images As I1
                        Join combo As C1
                            On C1.sku = I1.sku
                    Where I1.stat = 1
                    Group By I1.sku
                    ) As Z
            Group By Z.sku
            )  As Counts
        On Counts.sku = I.sku
Where stat = 0

显然在所有情况下,替换为表PrimaryKeyCol的实际主键列的名称images

于 2012-05-02T19:22:14.820 回答
1

您是否考虑过更改WHERE逻辑以执行计数。假设这两个计数是互斥的,你可以只OR考虑这两个条件。

于 2012-05-02T19:16:29.857 回答
1

使用交叉连接。. .

select t1.*, t2.total1, t3.total2
from 
(
  SELECT I.id, I.custname, I.custemail, I.sku,
      DATE_FORMAT(FROM_UNIXTIME(I.ts), '%l:%i:%s %p, %c/%e/%Y') AS ts
  FROM images I
  WHERE I.stat = 0
) t1 
cross join
(
  SELECT COUNT(*) AS total1
  FROM images
  WHERE stat = 1 AND sku = ?
) t2 
cross join
(
  SELECT COUNT(*) AS total2
  FROM images
  WHERE stat = 1  AND sku IN (SELECT subsku FROM combo WHERE sku = ?) 
) t3

您也许可以提高效率,因为它们都在同一张桌子后面。每个子句带有 case 语句的单个聚合WHERE可能会更有效。

于 2012-05-02T19:18:27.147 回答