问题
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