MySQL - 结合多个查询+计数

时间:2012-05-02 19:09:02

标签: mysql sql count subquery

我正在尝试合并一些查询,似乎无法确定下来。我想知道是否有人能指出我正确的方向。

以下是陈述:

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+行)与标量。如果我能弄清楚那部分,我认为这会奏效。

3 个答案:

答案 0 :(得分: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

修改

如果您正在寻找计数 by image ,其中您将计数与外表images sku列相关联,那就完全不同了。为此,我将使用派生表:

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表的实际主键列的名称。

答案 1 :(得分:1)

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

答案 2 :(得分: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

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