sql syntax, if exist

。_饼干妹妹 提交于 2019-12-24 01:23:22

问题


why i get error:

#1064 - 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 'IF EXISTS(SELECT id FROM mytable WHERE id = '1')' at line 1 

query:

IF EXISTS(SELECT id FROM mytable WHERE id = '1')

Thanks


回答1:


IF EXISTS only works in a stored procedure. Outside of a stored procedure, IF() is a function which takes 3 arguments. Proper usage would be

SELECT IF(EXISTS(SELECT `column` FROM `table` WHERE `id` = `1`), 1, 0);



回答2:


Not on a MySQL machine right now but it looks like its because the statement is incomplete, you need to tell it what to do if the id exists.

IF EXISTS(...) do something




回答3:


IF EXISTS doesn't make any sense in MySQL. See subqueries with EXISTS or NOT EXISTS in the MySQL documentation on usage.

Basically, you need to use it in a statement, and not just like a logical block




回答4:


Try to use

Declare @ID Integer
Select @ID=id From mytable where id=1
IF @ID is not null    OR  IF @ID > 0
Begin
....
End


来源:https://stackoverflow.com/questions/3241563/sql-syntax-if-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!