我从一个表中获取数据,并尝试将其插入到另一个表中,如果数据不存在,当“not exists”只返回一行时,这种方法可以工作,但如果它返回多行,则会失败。
以下是我的sql:
INSERT INTO datatables.SoftwareProjects_Tasks (Description, Project_ID,
Task_Status)
SELECT * FROM (SELECT Task FROM datatables.DefaultTask Where Department_Id =
5) as a, (SELECT sp.id FROM datatables.SoftwareProjects AS sp
INNER JOIN S4Admin.Project AS p ON sp.s4_project_id = p.Project_Code
WHERE p.Project_Code = 2294) as b, (SELECT 3) as c
WHERE NOT EXISTS (
SELECT Description, Project_ID FROM SoftwareProjects_Tasks WHERE Description
= (SELECT Task FROM datatables.DefaultTask Where Department_Id = 5) AND
Project_ID = (SELECT sp.id FROM datatables.SoftwareProjects AS sp
INNER JOIN S4Admin.Project AS p ON sp.s4_project_id = p.Project_Code
WHERE p.Project_Code = 2294)
) LIMIT 1;
1条答案
按热度按时间im9ewurl1#
我想麻烦来了
我认为你的问题不正确。我建议试着用另一种方法
你可以试着用
MINUS
如果你的mysql版本支持的话请参阅以下关于
MINUS
在mysql中-http://gokhanatil.com/2010/10/minus-and-intersect-in-mysql.html还有一个变种
NOT EXISTS
```--INSERT INTO datatables.SoftwareProjects_Tasks (Description, Project_ID, Task_Status)
SELECT t.Task,p.Project_ID,3 AS Task_Status
FROM datatables.DefaultTask AS t,
(
SELECT sp.id AS Project_ID
FROM datatables.SoftwareProjects AS sp
JOIN S4Admin.Project AS p ON sp.s4_project_id = p.Project_Code
WHERE p.Project_Code=2294
) AS p
WHERE t.Department_Id=5
AND NOT EXISTS(
SELECT *
FROM SoftwareProjects_Tasks b
WHERE b.Description=t.Task AND b.Project_ID=p.Project_ID
)