查询人员表,按职位分组,取组内id最大的记录:
1.
2.
最简单的就是
[size=large][b]再麻烦一点:[/b][/size]
查询工具表中,所有分组中版本(version)最新的,并且是使用(state)中的工具:
1.
2.
第二种方式严重不合理,当有相同版本时就不适用。建议使用第一种
有更好的方法的话希望可以分享。谢谢
1.
SELECT * FROM person p where not exists (SELECT * FROM person where post_id = p.post_id and id > p.id)
.
2.
select * from person where id in (SELECT max(id) FROM person group by post_id)
最简单的就是
SELECT max(id) FROM person group by post_id
[size=large][b]再麻烦一点:[/b][/size]
查询工具表中,所有分组中版本(version)最新的,并且是使用(state)中的工具:
1.
select * from tool t where state = 0 and not exists(select * from tool where state = 0 and name = t.name and version >t.version)
2.
select * from Tool where version in (SELECT max(version) FROM person where state = 0 group by name)
第二种方式严重不合理,当有相同版本时就不适用。建议使用第一种
有更好的方法的话希望可以分享。谢谢