求SQL代码分析过程
User_Id name
1 1
2 1
3 2
4 3
5 2
6 3
7 4
8 5
表名:Bse_User,删除重复的Name中User_Id比较小的记录。
------解决方案--------------------delete from bse_user a
where exists (select 1 from (select min(user_id)userid,name from bse_user group by name) b
where a.user_id=b.user_id and a.name=b.name)
------解决方案--------------------delete a
from (select *,row_number()over(partition by Name order by User_ID desc) as Row from Bse_User) as a
where Row>1
SQL2005以上版本可以这样用
------解决方案--------------------看来你没明白max/min/count/avg/sum这五个的用法和sql查询的顺序,
select (User_Id) from Bse_User group by Name having count(*)>1这句,先from,在group by,再select,再having,group by只出现了name,那么你在select的之后只能出现name,而其他字段要用上面五个函数的其中一个或一些来包住,不然肯定报错。