views:

92

answers:

4

Hi,

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

A: 

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
Waleed A.K.
That shouldn't matter. `WHERE id = '1'` will work fine even if `id` is an integer.
Mark Byers
I agree with Mark Byers - MySQL does implicit data type conversion between INT and string data types.
OMG Ponies
Thanks for your comment, you are right, I've updated my post.
Waleed A.K.
A: 

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

kekekela
A: 

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

ctshryock
IF EXISTS is often used in SQL Server, because it's less verbose than `IF(SELECT COUNT()...) > 0` etc.
OMG Ponies
+1  A: 

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);
Exception