SQL distinct 返回多列结果

来源:百度知道 编辑:UC知道 时间:2024/06/25 11:54:06
表中有sn,status,datetime.
select distinct sn from 表 where status='pass'
返回只能显示一列sn记录
我想显示出全部列的记录
sn status datetime
1 pass 09-01-20
1 fail 09-02-10
2 pass 09-03-09
2 pass 09-03-10
1 pass 09-03-20
2 fail 09-02-10
3 fail 09-02-10

结果为:
1 pass 09-03-20
2 pass 09-03-10
datetime 为最后一次pass的结果。
我的意思是 就是要去除重复的部分,然后把结果显示出来
我那样只能显示 sn一列,我想要显示全部列数据。

参考答案· 在命运的颠沛中,最容易看出一个人的气节。

把distinct去掉就行了,distinct是去除重复的返回数据

你如果要查询每个人最后一次过关的时间和sn,这样写没错:
select sn, max(datetime) from 表 where status='pass' group by sn

select * from 表 where status='pass' order by datetime desc,sn asc