MySQL的INSERT:SQL UPDATE或者如果行不存在
"UPDATE FYP_MilestoneNotes SET Notes = '$notes' WHERE UserID = $userID AND MilestoneID = $milestoneID"
當該行不存在,我得到預期的錯誤,我怎樣才能改變這種插入一行,如果相同的價值觀一個不存在?
我已經試過:
"INSERT INTO FYP_MilestoneNotes SET Notes = '$notes'
WHERE UserID = $userID
AND MilestoneID = $milestoneID
ON DUPLICATE KEY UPDATE FYP_MilestoneNotes
SET Notes = '$notes'
WHERE UserID = $userID
AND MilestoneID = $milestoneID
"
沒有運氣
我也嘗試下面的建議:
您的SQL語法錯誤;檢查手冊中 對應於你的MySQL服務器版本正確的語法使用 附近 '其中userid = 2,MilestoneID = 11 DUPLIC' 在行1
INSERT INTO FYP_MilestoneNotes(注)VALUES( 'sdgfdgfd') WHERE 用戶ID = 2並且MilestoneID = 11 ON DUPLICATE KEY UPDATE 註釋= 'sdgfdgfd' 其中userid = 2且MilestoneID = 11個
快速谷歌搜索顯示:http://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html – apokryfos
您想要使用INSERT INTO ... ON DUPLICATE KEY UPDATE文檔:http://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html – Mikey
[「INSERT IGNORE」vs「INSERT ... ON DUPLICATE KEY UPDATE」](http: //www.stackoverflow.com/questions/548541/insert-ignore-vs-insert-on-duplicate-key-update) – chris85