我寫此查詢,但我得到了以下錯誤:爲什麼這個SQL查詢不起作用?
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 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 'BEGIN INSERT INTO
forum_topics_track
(userid
,topic_id
, `c' at line 3
我想這是自我解釋,但我的目標是檢查記錄存在,如果沒有,將其插入。
IF NOT EXISTS
(SELECT * FROM `forum_topics_track` WHERE `userid` = '{$userid}' AND `topic_id` = '{$topic_id}')
BEGIN
INSERT INTO `forum_topics_track` (`userid`, `topic_id`, `category_id`)
VALUES ('{$topic_id}', '{$category_id}', '{$userid}')
END;
沒有必要先檢查任何東西。事實上,這完全適得其反。 – Strawberry