Comments - CASE OPERATOR

4 months, 1 week ago Jindrich Pachta

Keep on mind that in complex CASE constructions the first matching WHEN is processed only.

So the CASE operator works as if-elseif-else construction, see following examples where each condition of WHEN compare_value is TRUE but only first is matched and its result is returned.

-- First example
SELECT 
  CASE true 
     WHEN (1=1) THEN '1=1' -- result is returned 
     WHEN (1=1 OR 2=2) THEN '1=1 OR 2=2' -- condition not processed
     ELSE 'else'
  END 
;
+-------------------------------------------------------------------------------------+
| CASE true WHEN (1=1) THEN '1=1' WHEN (1=1 OR 2=2) THEN '1=1 OR 2=2' ELSE 'else' END |
+-------------------------------------------------------------------------------------+
+ 1=1                                                                                 +
+-------------------------------------------------------------------------------------+

-- Second example
SELECT 
  CASE true 
     WHEN (1=1 OR 2=2) THEN '1=1 OR 2=2' -- result is returned 
     WHEN (2=2) THEN '2=2' -- condition not processed
     ELSE 'else'
  END 
;
+-------------------------------------------------------------------------------------+
| CASE true WHEN (1=1 OR 2=2) THEN '1=1 OR 2=2' WHEN (2=2) THEN '2=2' ELSE 'else' END |
+-------------------------------------------------------------------------------------+
+ 1=1 OR 2=2                                                                          +
+-------------------------------------------------------------------------------------+
 
4 months, 1 week ago Ian Gilfillan

Thanks, clarified in the text.

 
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.