View Single Post

   
  #2 (permalink)  
Old 02-28-2008, 07:37 AM
Giuseppe Maxia
 
Posts: n/a
Default Re: Simple IF, ELSE in SQL

Lee wrote:
> I'm new to MySQL and am have trouble with conditionals (IF/ELSE)
>
> Trying the simplest possible query:
>
> IF EXISTS (SELECT 1) SELECT 4 ELSE SELECT 1;
> IF EXISTS (SELECT 1) THEN SELECT 4 ELSE SELECT 1;
> IF EXISTS (SELECT 1) THEN SELECT 4 ELSE SELECT 1; ENDIF;
> IF EXISTS (SELECT 1) THEN SELECT 4 ELSE SELECT 1; END IF;
> (I execute these separately, just trying to show some permutations I
> thought of)
>
> or any variation I can think of involving IF and ELSE. Using actual
> data and tables yields the same results. ("You have an error in you
> MySQL syntax" - helpful)
>
> I'm using MySQL 5.0.19 (same syntax works in SQL 2000)
>
> Thanks for any help!
>


You can use IF statements only inside stored routines.

http://dev.mysql.com/doc/refman/5.0/...statement.html

delimiter //
create procedure test1()
begin
IF EXISTS (SELECT 1) THEN
SELECT 4;
ELSE
SELECT 1;
END IF;
end //
delimiter ;

call test1();
+---+
| 4 |
+---+
| 4 |
+---+

This article covers stored routines in MySQL and the differences between MySQL and other SQL dialects.
http://dev.mysql.com/tech-resources/...rocedures.html

ciao
gmax

--
_ _ _ _
(_|| | |(_|>< The Data Charmer
_|
http://datacharmer.blogspot.com/
Reply With Quote