当前位置: 代码迷 >> Sql Server >> sql语句查询去除重复不用distinct用having解决办法
  详细解决方案

sql语句查询去除重复不用distinct用having解决办法

热度:19   发布时间:2016-04-27 13:41:20.0
sql语句查询去除重复不用distinct用having
id name sex add mobile
1 2 2 2 2
2 2 2 3 4
3 4 3 4 5


根据name去掉重复只留一条查询结果如下
id name sex add mobile
1 2 2 2 2
3 4 3 4 5
或者
id name sex add mobile
2 2 2 3 4
3 4 3 4 5


------解决方案--------------------
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 )
  相关解决方案