SQL syntax for "if exists"
Why I'm getting this error:
#1064 - You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version
for the right syn开发者_如何学Ctax to use near
'IF EXISTS(SELECT id FROM mytable WHERE id = '1')' at line 1
My SQL query:
IF EXISTS(SELECT id FROM mytable WHERE id = '1')
Thanks.
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);
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
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
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
精彩评论