我写此查询,但我得到了以下错误:为什么这个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