代码不存在的错误 - mariaDB
Error with not exists code - mariaDB
我不确定为什么:
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');
INSERT 语句没有 WHERE 子句。如果你想 运行 条件 INSERT 语句,你可以使用带有虚拟 table:
的 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')
做起来更简单(也更快)
INSERT IGNORE INTO $db.further_assess
(taskid)
VALUES
('$id')
我不确定为什么:
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');
INSERT 语句没有 WHERE 子句。如果你想 运行 条件 INSERT 语句,你可以使用带有虚拟 table:
的 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')
做起来更简单(也更快)
INSERT IGNORE INTO $db.further_assess
(taskid)
VALUES
('$id')