假如,我现有如下表格TVShow:
id | serialNo | title | status |
10001 | 1 | 中国好声音 | 1 |
10001 | 2 | 中国好声音 | 1 |
10001 | 3 | 中国好声音 | 1 |
10002 | 1 | 天天向上 | 1 |
10002 | 2 | 天天向上 | 1 |
10002 | 3 | 天天向上 | 1 |
10003 | 1 | 非诚勿扰 | 1 |
10003 | 2 | 非常勿扰 | 1 |
现在想用一条select语句实现如下的查询结果:
id | serialNo | title | status |
10001 | 1 | 中国好声音 | 1 |
10002 | 1 | 天天向上 | 1 |
10003 | 1 | 非诚勿扰 | 1 |
也就是不让显示重复的id,但,其它字段必须都得显示出来。我试过用distinct,但还是没能达到我想要的这种效果~求解?谢谢了~
加条件serialNo=1 不行吗?
select id,serialNo,distinct title, status from TVShow
select * from TVShow group by id
SELECT id,SUM(serialNo) as serialNo,MIN(title) AS title,SUM(status) as status FROM TVShow GROUP BY id;呵呵 好好利用group by,distinct好像解决不了你的问题
请使用开窗函数。
方法一:开窗函数
方法二:用子查询(其中B.serialNo<A.serialNo 这个条件由自己的需要定)
select * from TVShow A
where not exists ( select 1 from TVShow B where B.id=A.id and B.serialNo<A.serialNo ) 2012-10-31 13:49
select t.* from TVShow t
where
t.serialNo in (select min(serialNo) from TVShow)
order by t.serialNo desc,t.id asc