为什么在mysql中此查询中出现语法错误

时间:2019-12-05 00:28:23

标签: mysql

DB的新手,我正在编写查询,但需要帮助来找出语法错误。 查询:

SELECT IF (EXISTS(SELECT * FROM contact_address WHERE ad_ct_id = 1)) THEN
BEGIN
UPDATE contact_address set ad_city = 'Iraq'
END
ELSE
BEGIN
INSERT INTO contact_address (ad_ct_id, ad_type, ad_city)
VALUES (1, 'o', 'Iraq')
END
END IF;

错误:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') THEN
BEGIN
UPDATE contact_address set ad_city = 'Iraq'
END
ELSE
BEGIN
INSERT I' at line 1

谢谢

2 个答案:

答案 0 :(得分:0)

删除SELECT

IF (EXISTS(SELECT * FROM contact_address WHERE ad_ct_id = 1)) THEN
BEGIN
    UPDATE contact_address set ad_city = 'Iraq'
END
ELSE
BEGIN
    INSERT INTO contact_address (ad_ct_id, ad_type, ad_city)
    VALUES (1, 'o', 'Iraq')
END
END IF;

答案 1 :(得分:0)

'为什么我会出现语法错误'-如果存储程序不在,则无法使用if..end,mysql不需要在if..end if内使用begin..end if,if语法应该为if存在(选择。 。),每个语句都需要终止。