转换此选择/加入查询以删除匹配的记录

时间:2011-11-16 15:46:15

标签: mysql

如何转换以下查询以删除与ProjectID / Project_ID匹配的记录为14?

select * from Project join Project_Assigned 
where Project.Project_ID = Project_Assigned.ProjectID 
and Project.Project_ID = 14;

select语句有效但当我尝试将其转换为删除匹配的记录时,我得到ERROR 1064(42000)。

delete * from Project join Project_Assigned 
where Project.Project_ID = Project_Assigned.ProjectID 
and Project.Project_ID = 14;

2 个答案:

答案 0 :(得分:0)

试试这个

DELETE FROM Project
WHERE Project_ID = 14
  AND Project_ID IN
    (SELECT ProjectID FROM Project_Assigned
     WHERE ProjectID = 14)

答案 1 :(得分:0)

您收到第一个错误,因为delete不能包含join子句。

我认为你不能只在一个查询中做到这一点。您需要使用Project_Assigned和Project考虑所有相关表,并查看其外键并首先删除其记录。 此外,由于外键,您必须在从Projects中删除记录之前从Project_Assigned中删除记录。