在存储过程中,调用:
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;
DELETE a1, a2 FROM db1.t1 AS a1 INNER JOIN db2.t2 AS a2
WHERE a1.id = a2.id;
DELETE a1, db2.a2 FROM db1.t1 AS a1 INNER JOIN db2.t2 AS a2
WHERE a1.id = a2.id;
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 ...
3.当前,我们还不能在删除表的时候,在子查询中select from相同的表。