从一个带有连接的表中删除

时间:2021-01-13 03:34:58

I'm trying to delete records from one database based on a selection criteria of another. We have two tables, emailNotification which stores a list of jobs and emails. Then we have jobs. I want to clear out emailNotifications for jobs that have been closed. I found some earlier examples on * that lead me to this type of syntax (I was previously trying to do the join before the where).

我正在尝试根据另一个数据库的选择标准从一个数据库中删除记录。我们有两个表,emailNotification,用于存储作业和电子邮件列表。然后我们有工作。我想清除已关闭的作业的电子邮件通知。我在*上找到了一些早期的例子,它们引导我使用这种类型的语法(我之前尝试在where之前进行连接)。

DELETE FROM emailNotification
WHERE notificationId IN (
 SELECT notificationId FROM emailNotification e
 LEFT JOIN jobs j ON j.jobId = e.jobId
WHERE j.active = 1
)

I'm getting the error, you can't specify the target table 'emailNotication' for update in the FROM Clause.

我收到错误,您无法在FROM子句中指定目标表'emailNotication'以进行更新。

3 个解决方案

#1


75  

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

#2


6  

MySQL DELETE records with JOIN

MySQL使用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:

通常在SELECT语句中使用INNER JOIN从表中选择具有其他表中相应记录的记录。我们还可以使用带有DELETE语句的INNER JOIN子句来删除表中的记录以及其他表中的相应记录,例如,要删除满足特定条件的T1和T2表中的记录,请使用以下语句:

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.

请注意,您将表名T1和T2放在DELETE和FROM之间。如果省略T1表,则DELETE语句仅删除T2表中的记录,如果省略T2表,则仅删除T1表中的记录。

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

连接条件T1.key = T2.key指定T2表中需要删除的相应记录。

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

WHERE子句中的条件指定需要删除T1和T2中的哪些记录。

#3


3  

You could try something like the following:

您可以尝试以下内容:

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

#1


75  

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

#2


6  

MySQL DELETE records with JOIN

MySQL使用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:

通常在SELECT语句中使用INNER JOIN从表中选择具有其他表中相应记录的记录。我们还可以使用带有DELETE语句的INNER JOIN子句来删除表中的记录以及其他表中的相应记录,例如,要删除满足特定条件的T1和T2表中的记录,请使用以下语句:

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.

请注意,您将表名T1和T2放在DELETE和FROM之间。如果省略T1表,则DELETE语句仅删除T2表中的记录,如果省略T2表,则仅删除T1表中的记录。

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

连接条件T1.key = T2.key指定T2表中需要删除的相应记录。

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

WHERE子句中的条件指定需要删除T1和T2中的哪些记录。

#3


3  

You could try something like the following:

您可以尝试以下内容:

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