日期:2014-05-20  浏览次数:20401 次

一道经典的sql面试题!认为自己sql玩得挺转的进来!
学号(自动编号)                   姓名             性别                 年龄

0001                                         xw                 男                     18

0002                                         mc                 女                     16

0003                                         ww                 男                     21

0004                                         xw                 男                     18

请写出实现如下功能的SQL语句:
删除除了学号(自动编号)字段以外,其它字段都相同的冗余记录!

------解决方案--------------------
方法一、delete table where 学号(自动编号) not in
(select max(学号(自动编号)) as xh , 姓名 , 性别 , 年龄
from table
group by 姓名 , 性别 , 年龄) A
方法二、
delete table where 学号(自动编号) not in (
select max(a.学号) from table B,(select distinct 姓名 , 性别 , 年龄 from table ) A
where A.姓名=b.姓名 and a.性别=b.性别 and a.年龄 = b.年龄
) C

以上方法不知道是否可以,没有在SQL中测试。



------解决方案--------------------
delete from tablename where 学号(自动编号) notexists (select distinct 姓名,性别,年龄from tablename)

------解决方案--------------------
lxmfll2000(lxm) 的,没分清in 和exists啦...

lhx1977(清水无鱼)的思路是正确的,调试后的写法如下:(注意in的子查询只能select出一列)
DELETE FROM table1
WHERE (学号 NOT IN
(SELECT MAX(学号) AS xh
FROM TABLE1
GROUP BY 姓名, 性别, 年龄))
------解决方案--------------------
DELETE FROM Test2
where [学号] in
(select [学号]=min([学号]) from Test2 group by [姓名],[性别],[年龄] having count(*)> 1)


------解决方案--------------------
create table #temp
(学号 int)
insert into #temp
(select [学号]=min([学号]) from Test2 group by [姓名],[性别],[年龄]
delete from Test2 where 学号 not in (select 学号 from #temp)
drop #temp
------解决方案--------------------
create table #temp
(学号 int)
insert into #temp
(select [学号]=min([学号]) from Test2 group by [姓名],[性别],[年龄])
delete from Test2 where 学号 not in (select 学号 from #temp)
drop table #temp
更正