请大家帮我看一下这条SQL语句怎么写?

来源:百度知道 编辑:UC知道 时间:2024/05/06 16:41:14
id leixing
1 a
2 b
3 c
4 d
5 a
6 a
每个leixing,我只想要一条记录,并且是id最大哪一条,sql语句怎么写呢?
假设表的名字为table1,
abingpow:你的有点象了,但是在第2个select里面查询字段没有leixing,可以用group by它么?

select * from table1 where id in (
select max(id) from table1 group by leixing
) order by leixing
当leixing 字段为空的时候 就算一种类型,就是说所有为空的 是一组
这样排序 order by leixing null组在最上面

看来不止一个人误解你的意思 ,你的问题需要仔细看才知道你要什么语句

select top 1 * from table1 order by ID desc
这样出来的就是最大的ID 那条

select * from table1 where id in (
select max(id) from table1 group by leixing
)

关键就是order by 得出以id字段的降序排列,然后通过select top 1 来选第一个记录

tao_3000的是正解 (我原本就是这么的意思...)