Reputation: 3823
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 syntax 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.
Upvotes: 4
Views: 9302
Reputation: 1656
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
Upvotes: 0
Reputation: 1257
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);
Upvotes: 8
Reputation: 11342
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
Upvotes: 0
Reputation: 9759
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
Upvotes: 0