Page 1 of 1

IF Statement

Posted: Wed Mar 23, 2005 1:14 am
by anjanesh
Need to use pure SQL (MySQL) statements for these - No PHP involved because this needs to be portable on other languages - with common MySQL db.

Code: Select all

IF (SELECT Category,Name FROM Category WHERE Name='Cat2')
THEN
ELSE
INSERT INTO Category VALUES ('','Cat2');
What Im trying to do is : If Cat2 doesnt exist in the Category table then add it.
My IF statement doesnt seem to be working.
IF (SELECT * FROM Category WHERE Name='Cat1') THEN SELECT 'a';
is showing error.
The doc http://dev.mysql.com/doc/mysql/en/if-statement.html has no examples.

Any idea to fix this ?
Thanks

Posted: Wed Mar 23, 2005 7:52 am
by feyd
stored procedure or function?

Posted: Wed Mar 23, 2005 8:00 am
by anjanesh
None. I just typed this as an SQL statement in phpMyAdmin.