0

I am trying to do a simple if/else clause in sql (Running with mysql/phpmyadmin). But I get 1064 - You have an error in your SQL syntax

SET @age = 50;

IF @age >  60
THEN 
Select `table1`.`Name` FROM table1 WHERE `Age` = 'Old'
ELSE
Select `table1`.`Name` FROM tablel WHERE `Age` = 'NotSoOld'
END IF;

What is wrong?

droidgren
  • 6,898
  • 8
  • 31
  • 27
  • 1
    Don't know. You could rewrite as `Select table1.Name FROM tablel WHERE Age = CASE WHEN @age > 60 THEN Old ELSE NotSoOld END` though – Martin Smith Dec 31 '10 at 11:32
  • MySQL does have some very informative error messages. It's great, especially when you have 700 lines of SQL code, with `an error in its syntax` – Alex Dec 31 '10 at 11:37

2 Answers2

2

tablel (L minus) instead of table1 (the number one) ?

Stef
  • 3,691
  • 6
  • 43
  • 58
1

I figured it out with using the case statement instead. Thank for the clue Martin! SQL: IF clause within WHERE clause

Community
  • 1
  • 1
droidgren
  • 6,898
  • 8
  • 31
  • 27