我試圖獲取每組中具有最大值的行。每個組的最大值的行
我有這個表
+-------+-----------+--------------+-----------+
| nid | name | crated | grp_id |
+-------+-----------+--------------+-----------+
| 1 | RAND_NAME | 123 | 11 |
| 2 | EHllo | 111 | 11 |
| 3 | Stop by | 444 | 11 |
| 4 | Radr c | 555 | 11 |
| 5 | NAE | 666 | 22 |
| 6 | ABC | 1234 | 22 |
| 7 | RAND | 123 | 22 |
| 8 | YELLO | 444 | 22 |
| 9 | AAA | 555 | 33 |
| 10 | WWW | 1235 | 33 |
| 11 | ADF | 553 | 33 |
+-------+-----------+--------------+-----------+
所以,我想這個表
+-------+-------------+------------+-----------+
| nid | name | created | grp_id |
+-------+-------------+------------+-----------+
| 3 | Stop by | 555 | 11 |
| 6 | ABC | 1234 | 22 |
| 10 | WWW | 1235 | 33 |
+-------+-------------+------------+-----------+
這意味着我要搶的是每個組中最高的創造價值的行。該表將按grp_id分組。
我是這樣想的:
SELECT nid, name, created, grp_id
FROM table t
WHERE t.created = (SELECT MAX(t1.created) FROM table t1)
GROUP BY grp_id
ORDER BY grp_id
但是,它沒有發揮出來。我應該做些什麼來獲得每組中具有最高創造價值的三個不同的行?
感謝您瞭解我的糟糕解釋。
爲什麼是444> 555 GRP 11? – Sentinel
對不起,錯誤。改變了價值。 – PHG