如何 select 最大值和 return 多个条目
how to select max value and return many entries
假设我有这样一个 table :
theme module changed
0 1 1426070691
1 1 1426070300
0 1 1324014321
1 2 1241245585
1 1 1015421251
我需要一个 SQL 查询,returns 相同的主题和模块的最大值已更改:
theme module changed
0 1 1426070691
1 1 1426070300
1 2 1241245585
您可以使用left join
select
t1.* from table_name t1
left join table_name t2
on t1.theme = t2.theme
and t1.module = t2.module
and t1.changed > t2.changed
where t2.theme is null
http://dev.mysql.com/doc/refman/5.0/en/example-maximum-column-group-row.html
您只需要按 theme
和 module
对结果进行分组:
SELECT theme, module, MAX(changed)
FROM table_name
GROUP BY theme, module
假设我有这样一个 table :
theme module changed
0 1 1426070691
1 1 1426070300
0 1 1324014321
1 2 1241245585
1 1 1015421251
我需要一个 SQL 查询,returns 相同的主题和模块的最大值已更改:
theme module changed
0 1 1426070691
1 1 1426070300
1 2 1241245585
您可以使用left join
select
t1.* from table_name t1
left join table_name t2
on t1.theme = t2.theme
and t1.module = t2.module
and t1.changed > t2.changed
where t2.theme is null
http://dev.mysql.com/doc/refman/5.0/en/example-maximum-column-group-row.html
您只需要按 theme
和 module
对结果进行分组:
SELECT theme, module, MAX(changed)
FROM table_name
GROUP BY theme, module