使用Join的MySQL删除记录不起作用

时间:2019-06-25 10:12:18

标签: mysql sql join

我正在尝试使用JOIN删除记录,但是无法正常工作,因此我尝试使用IN来删除记录也无法正常工作。但是,如果我将DELETE更改为SELECT,则可以正常工作。

工作

SELECT * FROM T1 
     WHERE T1.ID IN (
     SELECT T1.ID FROM T1 
     JOIN T2 ON T2.PID = T1.ID WHERE T2.Email LIKE '%mitul%')

无法正常工作

DELETE FROM T1 
     WHERE T1.ID IN (
     SELECT T1.ID FROM T1 
     JOIN T2 ON T2.PID = T1.ID WHERE T2.Email LIKE '%mitul%')

DELETE FROM T1 
     JOIN T2 ON T2.PID = T1.ID WHERE T2.Email LIKE '%mitul%'

1 个答案:

答案 0 :(得分:4)

您可以在DELETE语句后提及表名或表别名:

DELETE T1
FROM T1 
JOIN T2 ON T2.PID = T1.ID 
WHERE T2.Email LIKE '%mitul%'

请在db<>fiddle上找到演示

如果要从两个表中删除匹配的条目,请在T1, T2语句后使用DELETE

DELETE T1, T2
FROM T1 
JOIN T2 ON T2.PID = T1.ID 
WHERE T2.Email LIKE '%mitul%'

Demo for the same