日期:2014-05-16  浏览次数:20949 次

删除表a在表b中不存在的字段
有两个表a,b其中有a有个字段id,与b的字段aid是相对应的,形成一对一对应,现在要删除表a中的id不在表b的aid的记录,要怎么写SQL。
谢谢

------解决方案--------------------
delete from a from a left join b on a.id=b.aid where b.aid is null;
------解决方案--------------------
delete from a where id not in(select a.id from a,b where a.id=b.aid);
------解决方案--------------------
delete a from a from a left join b on a.id=b.aid where b.aid is null;

一楼少写了个 a
------解决方案--------------------
SQL code
delete a from a left join b on a.id=b.aid where b.aid is null;

------解决方案--------------------
是不是因为id没指明是a表的,应该是delete from a where a.id not in(select a.id from a,b where a.id=b.aid);

------解决方案--------------------
delete from a where not exists(select 1 from b where a.id=aid);

------解决方案--------------------
SQL code
create table a(id int);
create table b (aid int);
insert a values (1),(2),(3);
insert b values(1);
delete from a where not exists(select 1 from b where a.id=aid);
select * from a 
+------+
| id   |
+------+
|    1 |
+------+

------解决方案--------------------
探讨

delete from a from a left join b on a.id=b.aid where b.aid is null;

------解决方案--------------------
探讨

引用:
delete from a from a left join b on a.id=b.aid where b.aid is null;


会报错:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server versi……

------解决方案--------------------
where isnull(b.aid)
是不是这样的?