如何使用group by获取MySql中的特定部分?

时间:2013-04-24 06:43:39

标签: php mysql group-by

我的SQL查询有问题,我会尝试解释我想要做什么。这是我的查询,它返回一些结果行。

SELECT 
sck.sckid,
sck.prid,
sck.paid,
sck.sckcen,
sum(scd.scdkiek) as count_of_goods,
min(scn.scndat) as `date`
FROM sck 
INNER JOIN scd ON scd.sckid = sck.sckid
INNER JOIN scn ON scn.scnid = scd.scnid
INNER JOIN sandeliai ON sandeliai.paid = sck.paid
WHERE sck.prid = 1376 GROUP BY sckid

此查询返回结果的“表1”:

表1:

sckid    |    prid    |    sckcen    |    count_of_goods    |      date     |
123      |    1376    |    10009     |          0           |   2012-12-31  |
124      |    1376    |    10007     |          15          |   2013-01-25  |
125      |    1376    |    10005     |          0           |   2013-02-13  |
126      |    1376    |    10000     |          18          |   2013-03-15  |

但我需要一行,所有数据按prid分组,我写了这个查询:

SELECT 
sck.sckid,
sck.prid,
sck.paid,
sck.sckcen,
sum(scd.scdkiek) as count_of_goods,
min(scn.scndat) as `date`
FROM sck 
INNER JOIN scd ON scd.sckid = sck.sckid
INNER JOIN scn ON scn.scnid = scd.scnid
INNER JOIN sandeliai ON sandeliai.paid = sck.paid
WHERE sck.prid = 1376 GROUP BY prid

然后我在表格中得到一行这个数据:

表2:

sckid    |    prid    |    sckcen    |    count_of_goods    |      date     |
123      |    1376    |    10009     |          23          |   2012-12-31  |

这似乎都是正确的,但在date字段中,我需要从表1返回最早的日期,其中包含count_of_goods> 0,所以对我来说需要这个结果:

表3:

sckid    |    prid    |    sckcen    |    count_of_goods    |      date     |
123      |    1376    |    10009     |          23          |   2013-01-25  |

所以任何想法如何得到表3中的结果?

3 个答案:

答案 0 :(得分:1)

尝试使用having,例如

SELECT 
sck.sckid,
sck.prid,
sck.paid,
sck.sckcen,
sum(scd.scdkiek) as count_of_goods,
min(scn.scndat) as `date`
FROM sck 
INNER JOIN scd ON scd.sckid = sck.sckid
INNER JOIN scn ON scn.scnid = scd.scnid
INNER JOIN sandeliai ON sandeliai.paid = sck.paid
WHERE sck.prid = 1376 GROUP BY prid HAVING count_of_goods > 0;

这对你有用。这可能会跳过count = 0的某些行,我认为应该没问题(仅限我个人意见)。

请以此为出发点,而不是最终解决方案。

修改:使用别名

更新了总和(...)

答案 1 :(得分:1)

我认为我最初回来的答案可能非常复杂。假设scd.scdkiek是数字而不是id,那么只需更改INNER JOIN标准即可得到你想要的东西,即

SELECT 
    sck.sckid,
    sck.prid,
    sck.paid,
    sck.sckcen,
    scn.scndat,
    SUM(scd.scdkiek) AS count_of_goods
FROM sck 
INNER JOIN scd 
    ON scd.sckid = sck.sckid
    AND scd.scdkiek > 0
INNER JOIN sandeliai 
    ON sandeliai.paid = sck.paid
INNER JOIN scn 
    ON scn.scnid = scd.scnid
WHERE sck.prid = 1376 
GROUP BY 
    sck.prid

虽然我会考虑使用子查询但是失败了。子查询将计算每行的count_of_goods。然后外部查询将使用它来决定是否将该行的日期设置为NULL。 MIN(... somedate ...,NULL)将返回日期。这反过来意味着您应该获得该行上count_of_goods不为NULL的最小日期。

当您查找count_of_goods为>的日期时,子查询按日期分组。 0

这当然只是一种选择,可能还有其他更有效的方法,但我相信这会像你的目标那样做

SELECT
    sck.sckid,
    sck.prid,
    sck.paid,
    sck.sckcen,
    SUM(d.count_of_goods) AS count_of_goods,
    MIN(IF(d.count_of_goods>0,d.scndat,NULL)) AS `date`
FROM (
    -- Get the data and group it by date
    SELECT 
        sck.sckid,
        sck.prid,
        sck.paid,
        sck.sckcen,
        scn.scndat,
        SUM(scd.scdkiek) AS count_of_goods
    FROM sck 
    INNER JOIN scd ON scd.sckid = sck.sckid
    INNER JOIN sandeliai ON sandeliai.paid = sck.paid
    INNER JOIN scn ON scn.scnid = scd.scnid
    WHERE sck.prid = 1376 
    GROUP BY 
        sck.prid, -- may not really be needed
        scn.scndat
) AS d
GROUP BY d.prid

答案 2 :(得分:0)

现在我的问题不合逻辑我试着在我的问题中结合两个查询,然后我得到一个很好的答案,这是我的解决方案:

SELECT 
sck.sckid,
sck.prid,
sck.paid,
sck.sckcen,
sum(scd.scdkiek) as count_of_goods,
(SELECT 
scn.scndat
FROM sck 
INNER JOIN scd ON scd.sckid = sck.sckid
INNER JOIN scn ON scn.scnid = scd.scnid
WHERE sck.prid = 1376 GROUP BY sck.sckid having sum(scd.scdkiek) > 0 ORDER BY scn.scndat LIMIT 1) as `date`
FROM sck 
INNER JOIN scd ON scd.sckid = sck.sckid
INNER JOIN scn ON scn.scnid = scd.scnid
WHERE sck.prid = 1376 GROUP BY prid;