尝试在不存在的位置插入数据抛出:#1242-子查询返回超过1行

yr9zkbsy  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(320)

我从一个表中获取数据,并尝试将其插入到另一个表中,如果数据不存在,当“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;
im9ewurl

im9ewurl1#

我想麻烦来了

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

你可以试着用 MINUS 如果你的mysql版本支持的话

--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

请参阅以下关于 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
)

希望我没听错。
也许你需要补充 `DISTINCT` 对于子查询 `Project_ID` ```
(
    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

相关问题