日期:2014-05-19  浏览次数:20356 次

有点意思的 SQL 查询语句,请高手指教 谢谢
3行数据,求查询结果
id     消息
1         q
2         q
3         w  
结果
1     q
3     w    

消息不能重复,同时查询出对应的ID

------解决方案--------------------
create table T(id int, 消息 varchar(10))
insert T select 1, 'q '
union all select 2, 'q '
union all select 3, 'w '
go

select * from T as A
where not exists(select 1 from T where 消息=A.消息 and id <A.id)

--result
id 消息
----------- ----------
1 q
3 w

(2 row(s) affected)