List:General Discussion« Previous MessageNext Message »
From:Alexander I. Barkov Date:October 11 1999 3:52pm
Subject:CASE WHEN .. THEN .. ELSE .. END
View as plain text  
Hi, MySQLers!

I have very easy implemented CASE in MySQL-3.22.27

According to ANSI SQL there is this structure:


CASE WHEN <boolean expression>
THEN <expression1>
ELSE <expression2>
END


I have added only two these in sql_yacc.yy at line 1196:

        | CASE WHEN expr THEN expr ELSE expr END
          { $$= new Item_func_if($3,$5,$7); }

and made all required changes in all related files.


Now I can do this:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2 to server version: 3.22.27

Type 'help' for help.

mysql> select case when 1<2 then 1 else 2 end;
+---------------------------------+
| case when 1<2 then 1 else 2 end |
+---------------------------------+
|                               1 |
+---------------------------------+
1 row in set (0.00 sec)

mysql> select case when 1>2 then 1 else 2 end;
+---------------------------------+
| case when 1>2 then 1 else 2 end |
+---------------------------------+
|                               2 |
+---------------------------------+
1 row in set (0.01 sec)




Or more complex sample:
mysql> select case when 1>2 then case when 3>4 then 4 else 5 end else 2 end;
+---------------------------------------------------------------+
| case when 1>2 then case when 3>4 then 4 else 5 end else 2 end |
+---------------------------------------------------------------+
|                                                             2 |
+---------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>





The question to developers. Could it be included in next
MySQL releases?



Thanks.

--- 
Alexander Barkov
IZHCOM, Izhevsk
email:    bar@stripped      | http://www.izhcom.ru
Phone:    +7 (3412) 51-55-45 | Fax: +7 (3412) 78-70-10
Thread
CASE WHEN .. THEN .. ELSE .. ENDAlexander I. Barkov11 Oct
  • Re: CASE WHEN .. THEN .. ELSE .. ENDTonu Samuel11 Oct