I keep getting this syntax error but can’t find anything wrong with it when comparing to other examples.
if EXISTS (select 1 from City where name = 'Perth')
THEN Print 'Record exits - Update'
ELSE Print 'Record doesn''t exist - Insert'
I find 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
1 from City where name = 'Perth') THEN Print 'Record e' at line 1
I get this both on zend cloud and normal phpmyadmin mysql 5
That isn’t actually a valid MySQL query. It looks like you are trying to mix together SQL with how you want to display output based on the whether the query exists or not. You can use this to return whether or not
Perthexists in SQL:This will return
1or0, which you can then parse with your server-side scripts. The reason it is giving you a syntax error is because MySQLIFstatements take the formIF(condition, <action if true>, <action if false>), with no use ofTHENorELSE(as is common in programming languages). Also, MySQL doesn’t have an explicitPRINTstatement, but you could useSELECTto somewhat accomplish what you want above (note that we can removeEXISTSas False will be implied if the result returns nothing):