我有两张表table1
和table2
。我想根据table1
中的条件从table2
中删除。
我有以下mysql查询:
DELETE FROM table1
INNER JOIN table2 ON table2.col1 = table1.col1
WHERE table2.col2 = '1'
这会返回语法错误。上面的语法有问题吗?
答案 0 :(得分:2)
您需要指定要删除的表格:
DELETE table1
FROM table1 INNER JOIN
table2
USING (col1)
WHERE table2.col2 = '1';
答案 1 :(得分:1)
试试这个:
DELETE FROM table1
WHERE EXISTS(
SELECT 'C'
FROM table2
WHERE table2.col1 = table1.col1
AND table2.col2 = '1'
)
答案 2 :(得分:1)
您可以执行以下操作:
DELETE FROM table1 WHERE col1 IN (select col1 from table2 WHERE table2.col2 = '1');