数据库问题

昨天招银电面被问的,如何删除一个没有主键的表中的所有重复数据,只要有重复就一条都不保留。
刚刚在mysql里面试,
create table tmp as SELECT id,name from test GROUP BY id,name HAVING count(*)>1;
SELECT * from test a where EXISTS (select * from tmp b where a.id=b.id and a.name=b.name);
drop table tmp;
这样是可以选出所有重复数据的,可是把select *改为delete为什么就显示错误呢?
全部评论
create temporary table tmp as SELECT test.name name,test.age age from test GROUP BY test.name,test.age HAVING count(*)>1; delete from test where EXISTS (select * from tmp where test.age=tmp.age and test.name=tmp.name); 根据楼主的语句改的。可行。
点赞 回复 分享
发布于 2016-09-22 18:34
create table test( name varchar(10) not null, age tinyint not null )charset=utf8; insert into test values("张三",22),("张三",22),("李四",23),("王五",24); create temporary table tmp like test; insert into tmp select * from test group by test.name,test.age having count(*) = 1; drop table test; create table test like tmp; insert into test select * from tmp; 只想到了这个笨方法。 不知道有没有更简单的。
点赞 回复 分享
发布于 2016-09-22 18:18
select distinct * into tmp from tableName   (得到无重复记录的表tmp) drop table tableName   select * into tableName from tmp drop table tmp 网上搜到这个做法,可以吗
点赞 回复 分享
发布于 2016-09-22 17:46
就算知道这么多命令,电面怎么口述回答啊?
点赞 回复 分享
发布于 2016-09-22 17:17
我也被招银问过这个问题
点赞 回复 分享
发布于 2016-09-22 17:09
为什么招银喜欢问sql语句
点赞 回复 分享
发布于 2016-09-22 17:07
根据rownum
点赞 回复 分享
发布于 2016-09-22 17:06
建立唯一索引就可以了
点赞 回复 分享
发布于 2016-09-22 16:43

相关推荐

no_work_no_life:深圳,充电宝,盲猜anker
点赞 评论 收藏
分享
评论
点赞
12
分享

创作者周榜

更多
牛客网
牛客企业服务