这就是我现在在查询中的内容:
+--------------------+-----------+------------+
| status | entity_id | seconds |
+--------------------+-----------+------------+
| Submitted | 494 | 1352102400 |
| Accepted | 494 | 1352275200 |
| In press/e-publish | 494 | 1352966400 |
| Rejected | 520 | 1355817600 |
| Accepted | 570 | 1352102400 |
+--------------------+-----------+------------+
我希望它看起来像:
+--------------------+-----------+------------+
| status | entity_id | seconds |
+--------------------+-----------+------------+
| In press/e-publish | 494 | 1352966400 |
| Rejected | 520 | 1355817600 |
| Accepted | 570 | 1352102400 |
+--------------------+-----------+------------+
在准SQL中:
SELECT status, entity_id, MAX(seconds)
FROM foo
GROUP BY entity_id, seconds
上面的准SQL看起来是正确的,但“status”列值与正确的行不对应。我得到了类似下面的内容:
+--------------------+-----------+------------+
| status | entity_id | seconds |
+--------------------+-----------+------------+
| Submitted | 494 | 1352966400 |
| Rejected | 520 | 1355817600 |
| Accepted | 570 | 1352102400 |
+--------------------+-----------+------------+
答案 0 :(得分:6)
未经测试,但应该看起来像这样:
SELECT status, entity_id, seconds
FROM entities E
WHERE E.seconds == (
SELECT MAX(E2.seconds)
FROM entities E2
WHERE E2.entity_id = E.entity_id
)
(为你的问题设置一个SQLfiddle会给你一个更加测试的答案:p)
答案 1 :(得分:3)
以下查询将给出您的预期结果。
SELECT max(maxsec), status, entity_id from
(SELECT status, entity_id, MAX(seconds) as maxsec
FROM table1
GROUP BY entity_id,status) a GROUP BY entity_id
您的架构已创建here。