日期:2014-05-18  浏览次数:20602 次

急求一查询语句。。.。感谢。。
现有表:
id     zd1     zd2     zd3
1       111     222     333
2       111     222     444
3       555     222     333
4       111     222     333
5       666     777     888
6       555     222     333
结果:
id     zd1     zd2     zd3
1       111     222     333
2       111     222     444
3       555     222     333
5       666     777     888

需求:查询出三个字段都唯一的第一条记录,去除重复的。我知道一个字段是distinct,多个可以用group   by   ,但没写出来。,急求各位帮忙。。感谢

------解决方案--------------------
select min(id)id,zd1,zd2,zd3 from tb group by zd1,zd2,zd3
------解决方案--------------------

select id=min(id),zd1,zd2,zd3
from 表
group by zd1,zd2,zd3
------解决方案--------------------
--如果是要刪除重復數據
Delete From 表 Where id Not In (Select Min(id) From 表 Group By zd1, zd2, zd3)
------解决方案--------------------
select min(id) as id,zd1,zd2,zd3 from tb group by zd1,zd2,zd3
------解决方案--------------------
create table tb(id int,zd1 int, zd2 int,zd3 int)
insert tb
select 1 , 111 ,222 ,333
union select 2 , 111, 222 ,444
union select 3 , 555 , 222 , 333
union select 4 , 111 ,222 , 333
union select 5 , 666 , 777 ,888
union select 6 , 555 ,222, 333

select id=min(id),zd1,zd2,zd3
from tb
group by zd1,zd2,zd3


drop table tb
/*
id zd1 zd2 zd3
----------- ----------- ----------- -----------
1 111 222 333
2 111 222 444
3 555 222 333
5 666 777 888

(4 row(s) affected)
*/