我有这个 SQL:
SELECT itemId, parentId, value FROM item ORDER BY value DESC
正确返回:
+--------+------------------+
| itemId | parentId | value |
+--------+------------------+
| 1 | 5 | 500 |
| 4 | 1 | 500 |
| 2 | 5 | 10 |
| 5 | 1 | 10 |
| 3 | 5 | 0 |
| 6 | 1 | 0 |
+--------+----------+-------+
我尝试添加“GROUP BY parentId”,但这似乎选择了两个随机项目,忽略了 ORDER BY 子句:
+--------+------------------+
| itemId | parentId | value |
+--------+------------------+
| 2 | 5 | 10 |
| 6 | 1 | 0 |
+--------+----------+-------+
我应该使用什么 SQL 来仅返回每个 parentId 具有最高值的项目?:
+--------+------------------+
| itemId | parentId | value |
+--------+------------------+
| 1 | 5 | 500 |
| 4 | 1 | 500 |
+--------+----------+-------+