我不确定原因:
INSERT INTO $db.further_assess (taskid) VALUES ('id')
WHERE NOT EXISTS (SELECT * FROM $db.further_assess where taskid='$id')
给了我这个错误:
You have an error in your SQL syntax; check the manual that corresponds to
your MariaDB server version for the right syntax to use near 'WHERE NOT
EXISTS (SELECT 1 FROM risk_assessment.further_assess where taskid='222' at line 2
是这样的:Sql insert if row does not exist
更新:
我的,现在正确,查询:
INSERT INTO $db.further_assess (taskid, reportid)
SELECT '$id', '$report_id'
FROM (SELECT 1) as dummytable
WHERE NOT EXISTS (SELECT * FROM $db.further_assess where taskid='$id');
答案 0 :(得分:1)
INSERT语句没有WHERE子句。如果要运行条件INSERT语句,可以使用带有伪表的INSERT-SELECT语句:
INSERT INTO $db.further_assess (taskid)
SELECT 'id'
FROM (SELECT 1) as dummytable
WHERE NOT EXISTS (SELECT * FROM $db.further_assess where taskid='$id')
答案 1 :(得分:1)
要做得更简单(也更快)
INSERT IGNORE INTO $db.further_assess
(taskid)
VALUES
('$id')