仔细看mysql-文档(12.2.2. delete syntax)手册应该不会出现这个问题,如果遇到请接着向下看: 在存储过程中,调用: delete a from db1.tb1 a, db2.tb2 b where a.col1 = b.col1 and a.col2 = b.col2; 出现题目中的错误,原因如下: 在mysql中多表联合删除
仔细看mysql-文档(12.2.2. delete syntax)手册应该不会出现这个问题,如果遇到请接着向下看:
在存储过程中,调用:
delete a from db1.tb1 a, db2.tb2 b where a.col1 = b.col1 and a.col2 = b.col2;
出现题目中的错误,原因如下:
在mysql中多表联合删除时,表别名只能在sql中表关联部分声明。我们应该避免不是表关联部分声明别名,因为这产生歧义的sql,从而产生不是期望的结果,例如在错误的表中删除行,举个例子来说:
delete t1 as a2 from t1 as a1 inner join t2 as a2;
对于多个表的删除,表的列表中提到的表别名的引用,使用默认数据库,除非已经明确地指定了一个数据库。例如,如果默认数据库是db1,下面的语句将不会工作,因为不确定的别名a2被认为已经有了db1:
delete a1, a2 from db1.t1 as a1 inner join db2.t2 as a2where a1.id=a2.id;
除了默认数据库外,为了正确地匹配表的别名,我们必须明确地指定正确的数据库的名字,
delete a1, db2.a2 from db1.t1 as a1 inner join db2.t2 as a2where a1.id=a2.id;
根据上述理论,我的sql应该这样写:
delete db1.a from db1.tb1 a, db2.tb2 b where a.col1 = b.col1 and a.col2 = b.col2;
还有需要注意的是:
1.如果你为一个表声明了别名,当你指向这个表的时候,就必须使用这个别名,例如:
-- 正确的写法: delete t1 from test as t1, test2 where ... -- 错误的写法:delete test from test as t1, test2 where ...
2.在多个表联合删除时,不能使用order by 或limit,而单个表的删除时就没有这个限制。
3.当前,我们还不能在删除表的时候,在子查询中select from相同的表。
原文:http://www.cnblogs.com/sunss/archive/2011/01/20/1940306.html
原文地址:unknown table ‘a’ in multi delete的解决办法和说明, 感谢原作者分享。
