What is wrong in below simple query? I tried to google it but not found
IF 1 = 1 THEN SELECT 1; ELSE SELECT 12; END IF;
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It only takes a minute to sign up.Sign up to join this community
I believe that syntax is actually valid; you can find similar statements in the MySQL documentation.
However, it's considered a control flow statement, and those are only allowed in stored programs in MySQL. You don't state that this is inside a stored program, so I'll assume it's not. If it is, please edit your question to provide more detail.
If you're trying to do this outside a stored program, see Evan Carroll's answer.
This simple query can't be run outside the stored routines as stated by chapter 13.6.5 of the manual. For the single queries
IF() function should be used instead.
CASE may be a bit confusing because the
CASE statement and the
CASE operator has exactly the same syntax, inspite of completely different nature.