我搜索了一会儿这个问题,但我无法解决它......
我想检查mysql表中是否存在记录,然后插入记录。
这是我的代码:
IF NOT EXISTS (SELECT * FROM personOffice WHERE personID = 2 AND officeID = 1)
BEGIN
INSERT INTO personOffice (personID, officeID) VALUES ('2', (SELECT officeID FROM offices WHERE title = 'Berlin'))
END
Mysql告诉我,我的第一行是语法错误。
谢谢你的解决方案。
答案 0 :(得分:0)
INSERT INTO personOffice(personID, officeID)
SELECT '2', (SELECT officeID FROM offices WHERE title = 'Berlin') FROM dual
WHERE NOT EXISTS (SELECT * FROM personOffice WHERE personId = 2 AND officeID = 1)
答案 1 :(得分:0)
你可以试试这个:
INSERT INTO personOffice (personID, officeID)
VALUES ('2', (SELECT officeID FROM offices WHERE title = 'Berlin'))
WHERE NOT EXISTS (SELECT * FROM personOffice WHERE personID = 2 AND officeID = 1)