IF Statement

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

IF Statement

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

stored procedure or function?
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

None. I just typed this as an SQL statement in phpMyAdmin.
Post Reply