尝试在不存在的地方插入数据抛出:#1242 - 子查询 returns 多于 1 行

Trying to insert data where not exists throwing: #1242 - Subquery returns more than 1 row

我正在从一个 table 获取数据并尝试将其插入另一个,如果数据尚不存在,这在 "not exists" 只有 returns 1 行时有效但如果它 returns 超过一个,它就会失败。

这是我的 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;

我觉得问题就出在这里

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)

我认为您的查询不正确。我建议尝试使用其他方式

--INSERT INTO datatables.SoftwareProjects_Tasks (Description, Project_ID, Task_Status)
SELECT a.Task,a.Project_ID,3 AS Task_Status
FROM
  (
    SELECT t.Task,p.Project_ID
    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
  ) a
LEFT JOIN SoftwareProjects_Tasks b ON b.Description=a.Task AND b.Project_ID=a.Project_ID
WHERE b.Project_ID IS NULL

如果您的 MySQL 版本支持,您可以尝试使用 MINUS 运算符

--INSERT INTO datatables.SoftwareProjects_Tasks (Description, Project_ID, Task_Status)
SELECT Task,Project_ID,3
FROM
  (
    SELECT t.Task,p.Project_ID
    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

    MINUS

    SELECT Description,Project_ID
    FROM SoftwareProjects_Tasks
  ) q

请参阅 MySQL 中有关 MINUS 的以下文章 - 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
    )

希望我理解正确。

也许您需要为带有 Project_ID

的子查询添加 DISTINCT
  (
    SELECT DISTINCT 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