首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-04-03 
sql语句查询去除重复不用distinct用havingidnamesexaddmobile122222223434345根据name去掉重复只留一条查

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 ) 

热点排行