0%

MySQL Delete

别名

delete使用别名的时候,要在delete和from间加上删除表的别名,这样才是正确的写法。

1
2
3
4
5
delete p,r from t_satisfaction_followup_patients p,t_hospital_followup_record r 
where p.plan_id = '7421aed79a77494d8b2f54c33f62dd2c'
AND r.plan_id = '7421aed79a77494d8b2f54c33f62dd2c'
AND r.submit_status = 0
AND p.id = r.relation_id

多表删除

您可以在DELETE语句中指定多个表,以 根据WHERE子句中的条件从一个或多个表中删除行 。不能使用ORDER BYLIMIT在多台 DELETE

1
2
DELETE t1, t2 FROM t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;
1
2
DELETE t1, t2 FROM t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;
1
DELETE t1 FROM t1 LEFT JOIN t2 ON t1.id=t2.id WHERE t2.id IS NULL;