Delete from one table with join

怎甘沉沦 提交于 2019-11-27 12:38:48
Naved

I am not sure about your requirement. What I understood from your question is you want to delete all the emails of jobs which are closed. try this one;

DELETE e FROM emailNotification e 
LEFT JOIN jobs j ON j.jobId = e.jobId 
WHERE j.active = 1 AND CURDATE() < j.closeDate

MySQL DELETE records with JOIN

Delete multiple records from multiple table using Single Query is As below:

You generally use INNER JOIN in the SELECT statement to select records from a table that have corresponding records in other tables. We can also use the INNER JOIN clause with the DELETE statement to delete records from a table and also the corresponding records in other tables e.g., to delete records from both T1 and T2 tables that meet a particular condition, you use the following statement:

DELETE T1, T2
FROM T1
INNER JOIN T2 ON T1.key = T2.key
WHERE condition

Notice that you put table names T1 and T2 between DELETE and FROM. If you omit the T1 table, the DELETE statement only deletes records in the T2 table, and if you omit the T2 table, only records in the T1 table are deleted.

The join condition T1.key = T2.key specifies the corresponding records in the T2 table that need be deleted.

The condition in the WHERE clause specifies which records in the T1 and T2 that need to be deleted.

You could try something like the following:

DELETE FROM emailNotification
WHERE jobId IN (
 SELECT jobId FROM jobs j
 WHERE j.active = 1
)

If the aim is deleting matching rows, like deleting rows in 1st table which have relations in 2nd, to avoid deleting whole 1st table you should put additional "where" condition for 2nd table

DELETE f FROM firsttable f 
LEFT JOIN secondtable s ON f.related_id = .jobId 
WHERE s.related_id
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!