我有一個SQL查詢的問題,我會嘗試解釋我想要做什麼。這是我的查詢返回一些結果行。如何在MySql中獲取組中的特定部分?
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
領域,我需要從表中的一個,其中有count_of_goods> 0回到最早期的日期,所以對我來說需要這樣的結果:
表3:
sckid | prid | sckcen | count_of_goods | date |
123 | 1376 | 10009 | 23 | 2013-01-25 |
所以,任何想法我怎麼能得到像表3中的結果?
它的數據類型,但對我來說最需要從較早的日期表1,其中count_of_goods> 0 – 2013-04-24 06:48:40
嘗試'MAX(scn.scndat)'。 – 2013-04-24 06:52:15
使用count_of_goods> 0按日期排序 – sandeepKumar 2013-04-24 07:09:28