使用内部联接删除查询

时间:2016-01-16 15:50:14

标签: mysql sql

查询

DELETE `contact`
FROM `contact` ct
INNER JOIN `contact_user` cu ON cu.contact_id=ct.id
WHERE ct.id=4 && ct.block_id=5671 && cu.user_id=1

错误

  

致命:SQLSTATE [42S02]:找不到基表或视图:1109未知   MULTI DELETE中的表'contact'; SQL:DELETE contact FROM contact   ct INNER JOIN contact_user cu ON cu.contact_id = ct.id WHERE ct.id = 4   &安培;&安培; ct.block_id = 5671&& cu.user_id = 1

2 个答案:

答案 0 :(得分:4)

由于您已为表Alias name定义了contact,因此您需要使用别名而不是原始表名。试试这个

DELETE ct 
FROM   contact ct 
       INNER JOIN contact_user cu 
               ON cu.contact_id = ct.id 
WHERE  ct.id = 4 
       AND ct.block_id = 5671 
       AND cu.user_id = 1 

或删除contact

的别名
DELETE contact 
FROM   contact 
       INNER JOIN contact_user cu 
               ON cu.contact_id = contact.id 
WHERE  contact.id = 4 
       AND contact.block_id = 5671 
       AND cu.user_id = 1 

答案 1 :(得分:4)

因为您已为表定义了别名,所以应该使用它。同时将&&替换为and

DELETE `ct` --`contact`
FROM `contact` ct
INNER JOIN `contact_user` cu ON cu.contact_id=ct.id
WHERE ct.id=4 and ct.block_id=5671 and cu.user_id=1