表 table
字段 id(唯一标识) name class date state
1 a 61 -- --
2 b 61 -- --
3 a 61 -- --
4 a 61 -- --使用 select distinct name,class from table where class=61
的话只能显示name,class这2个字段
如果我想所有字段的sql语句要怎么写?
效果-----------------------------------------------------
字段 id(唯一标识) name class date state
1 a 61 -- --
2 b 61 -- --
字段 id(唯一标识) name class date state
1 a 61 -- --
2 b 61 -- --
3 a 61 -- --
4 a 61 -- --使用 select distinct name,class from table where class=61
的话只能显示name,class这2个字段
如果我想所有字段的sql语句要怎么写?
效果-----------------------------------------------------
字段 id(唯一标识) name class date state
1 a 61 -- --
2 b 61 -- --
select * from table where (name,class)
in (select distinct name,class from table where class=61)
with t1 as (select a.*,row_number()over(partition by name ,class order by id asc) rn from table a)
select * from t1 where rn=1
你那种写法貌似行不通
能用一条sql语句搞定吗??
select * from tb a where not exists(select 1 from tb b where a.name=b.name and a.id>b.id)select *
from (select a.*,row_number() over(partition by a.name order by id) rn
from tb a)
where rn=1