2012-09-05
Select中DISTINCT一些用法?
假设是 table
id name
1 a
2 b
3 c
4 c
5 b
我想得到 id 和 name 但name不重复。直接 select distinct name from table 肯定不行。
可以如下方法:(主要是用mysql测试)
select *,count(distinct name) from table group by name order by id
select *,group_concat(distinct name) from table group by name order by id
select * from table where id in(select min(id) from table group by name) (mysql和sql server都pass)
另:
1.打开不重复任意次数的所有记录
select * from table where name in(select name from table group by name having count(name)=1)
2.打开重复记录
select * from table where name in(select name from table group by name having count(name)=2)
完