我正在尝试获取每个ID的顶级ID记录列表,但是当我输入此查询时:
select max(submitted), t.* from ods.events t where id in
(262, 331, 144, 664, 353, 641, 190, 527, 581) group by id;
我得到以下内容:
+---------------------+-----+---------------------+
| max(submitted) | id | submitted |
+---------------------+-----+---------------------+
| 2015-03-17 14:39:53 | 144 | 2014-04-14 04:57:09 |
| 2015-03-03 08:30:15 | 190 | 2014-04-14 01:51:01 |
| 2015-03-17 13:40:14 | 262 | 2014-04-14 03:56:03 |
| 2015-03-17 06:45:10 | 331 | 2014-04-14 05:51:02 |
| 2015-03-17 14:39:41 | 353 | 2014-04-14 05:50:30 |
| 2015-03-17 06:45:39 | 527 | 2014-04-13 21:38:14 |
| 2015-03-17 15:41:32 | 581 | 2014-04-14 05:54:43 |
| 2015-03-17 15:40:26 | 641 | 2014-04-14 05:52:10 |
| 2015-03-17 15:42:10 | 664 | 2014-04-14 05:53:44 |
+---------------------+-----+---------------------+
显然,它并没有带来与max(已提交)相关的整个记录,而是其他一些记录。如何引入与max(提交)相关的整个记录?
答案 0 :(得分:-1)
选择已提交的*,来自id in ... group by id已提交= max(已提交)
的事件答案 1 :(得分:-1)
SELECT t.*
FROM ods.events t
LEFT JOIN
ods.events
as t2
ON t.id=t2.id
AND t.submitted < t2.submitted
WHERE t.id IN (262, 331, 144, 664, 353, 641, 190, 527, 581)
AND t2.id IS NULL
GROUP BY t.id;
答案 2 :(得分:-1)
从事件t1中选择t1.submitted,t1。*,其中t1.id in(...)和t1.submitted = select max(t2.submitted)from events t2 where t1.id = t2.id group by t2 .ID