Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 一组信息亭的Max记录_Mysql_Sql_Aggregate Functions - Fatal编程技术网

Mysql 一组信息亭的Max记录

Mysql 一组信息亭的Max记录,mysql,sql,aggregate-functions,Mysql,Sql,Aggregate Functions,我试图获取每个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 | submit

我试图获取每个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(submitted)相关联的整个记录,而是其他一些记录。如何引入与max(已提交)关联的整个记录?

选择submitted,*from events where id in。。。按已提交id分组=最大值(已提交)

选择t.*
来自ods.t
左连接
ods.活动
as t2
在t.id=t2.id上
和t.提交
从事件t1中选择t1.submitted,t1.*其中t1.id在(…)中,t1.submitted=选择最大值(t2.submitted)从事件t2中,t1.id=t2.id按t2.id分组

您的
ods.events
是否只包含
id
submitted
,或者它们是您没有告诉我们的其他列?这不会返回任何结果为什么子查询在您可以将
左键连接到ods.events t2上时…
?是,你是对的,我可以直接连接而不需要子查询。计划在子查询中包含WHERE子句。:-)
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;