读书人

sql语句查询去除重复不用distinct用ha

发布时间: 2012-04-03 12:38:19 作者: rapoo

sql语句查询去除重复不用distinct用having
id name sex add mobile
12222
22234
34345


根据name去掉重复只留一条查询结果如下
id name sex add mobile
12222
34345
或者
id name sex add mobile
22234
34345


[解决办法]
select tb.* from tb,(
select MAX(id) as id from tb
group by tb.name)tb2
where tb.id=tb2.id
[解决办法]

SQL code
select * from tb  t where not exists(select 1 from tb where name=t.name and id>t.id)
[解决办法]
SQL code
--orselect * from tb  t where id=(select max(id)from tb where name=t.name ) 

读书人网 >SQL Server

热点推荐