MYSQL - 使用Join删除查询

时间:2013-02-22 14:26:48

标签: mysql

我检查了已回答的问题。但是,解决方案对我不起作用。

DELETE FROM TEST2
INNER JOIN TEST1 on TEST1.FIELD2 = TEST2.FIELD2
WHERE TEST1.FIELD1 = 22;

当我执行此查询时,我在phpmyadmin中收到以下错误。

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version 
for the right syntax to use near 'INNER JOIN TEST1 on TEST1.FIELD2 = TEST2.FIELD2' at line 2

我累了。我需要帮助。提前谢谢。

1 个答案:

答案 0 :(得分:17)

这应该有效:

DELETE T
FROM TEST2 T
INNER JOIN TEST1 on TEST1.FIELD2 = T.FIELD2
WHERE TEST1.FIELD1 = 22;

Sample Fiddle Demo

我认为你也可以用IN

来做
DELETE FROM Test2
WHERE Field2 IN (
    SELECT Field2 
    FROM Test1
    WHERE Field1 = 22)