我收到以下错误:
1064 - 您的SQL语法出错;查看与您的MySQL服务器版本相对应的手册,以便在#&; 39,WHERE NOT EXISTS附近使用正确的语法(SELECT * FROM training2subjects WHERE trainingID = 3 AND subj' at line 1
当我在mysql上查询这个查询时 -
INSERT INTO training2subjects ( trainingID, subjectID )
VALUES ( 3, 5 )
WHERE NOT EXISTS (
SELECT *
FROM training2subjects
WHERE trainingID = 3
AND subjectID = 5
LIMIT 0,1
)
当我运行子查询时,它可以正常工作
SELECT *
FROM training2subjects
WHERE trainingID = 3
AND subjectID = 5
LIMIT 0,1
我想仅在相同的数据不存在的情况下插入
答案 0 :(得分:0)
您需要使用INSERT INTO - SELECT
:
INSERT INTO training2subjects ( trainingID, subjectID )
SELECT 3, 5
WHERE NOT EXISTS (SELECT *
FROM training2subjects
WHERE trainingID = 3 AND subjectID = 5);
<强> DBFiddle Demo 强>