您的位置:首页 > 数据库 > MySQL

关于mysql删除语句的子查询问题

2016-06-15 16:42 507 查看
今天要删除letterrelation表中无效的数据,于是找出无效的letterrelationId

select letterRelationId from letterrelation where letterRelationId not in(select distinct letterRelationId from letter);


然后在前面加上delete语句

delete from letter where letterRelationId in(select letterRelationId from letterrelation where letterRelationId not in(select distinct letterRelationId from letter));


呵呵。报错了



于是后来百度好久,找到一个网址解答:http://zhidao.baidu.com/question/1831460432659159420.html

于是我就写下了自己的sql语句:

create table table_test as select letterRelationId from letterrelation where letterRelationId not in(select distinct letterRelationId from letter);
delete from letterrelation where letterRelationId in (select letterRelationId from table_test);
drop table table_test;


最后发现完美解决了。但是原因我说一下:

1网上的解释是说mysql工具不强大,无法做到对某个表进行查询了然后又进行删除操作,但是oracle可以做到。这样

算的话就是mysql不强大了。

2同事说是建立视图了,视图我不太了解,暂且等我先去研究一下
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql