List:General Discussion« Previous MessageNext Message »
From:Martin Ramsch Date:June 21 1999 3:12pm
Subject:Re: switch statement
View as plain text  
On Mo, 1999-06-21 10:51:36 -0400, Dave alias Para-dox wrote:
> does MySQL have a switch() function?
[...]
> switch(X='A', 1, X='B', 2, X='C', 3, X='D', 4, 1, 5) =
> IF(X='A', 1, IF(X='B', 2, IF(X='C', 3, IF(X='D', 4, 5))))

Search chapter 7.3 of the manual ("Functions for use in SELECT and
WHERE clauses") for similiar functions!

You could do this task by means of ELT() and FIELD().  Example:

  FIELD(X, 'A', 'B', 'C', 'D')  yields 1, 2, 3, or 4, respectively.

If it's not just index numbers you want, you could combine FIELD with
ELT.  Example:

  ELT( FIELD(X, 'A', 'B', 'C', 'D'), '1st', '2nd', '3rd', '4th' )

Regards,
  Martin
-- 
Martin Ramsch <m.ramsch@stripped> <URL: http://home.pages.de/~ramsch/ >
PGP KeyID=0xE8EF4F75 FiPr=52 44 5E F3 B0 B1 38 26  E4 EC 80 58 7B 31 3A D7
Thread
switch statementPara-dox21 Jun
  • Re: switch statementMartin Ramsch21 Jun