Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use "OR" condition in MySQL CASE expression?

I have a procedure that contains CASE expression statement like so:

BEGIN
....
WHILE counter < total DO
....

 CASE ranking
  WHEN 1 OR 51 OR 100 OR 167 THEN SET
   project_name = 'alpha';
  WHEN 2 THEN SET
   project_name = 'beta';
  WHEN 10 OR 31 OR 40 OR 61 THEN SET
   project_name = 'charlie';
  ....
  ELSE SET
   project_name = 'zelta';
 END CASE;

 INSERT INTO project (id, name) VALUES (LAST_INSERT_ID(), project_name);
 SET counter = counter + 1;

END WHILE;
END
$$
DELIMITER ;

When I call the above procedure, cases with OR statements are either skipped completely or only the first item in the list is matched. What am I doing wrong?

like image 821
Question Overflow Avatar asked Jul 08 '14 11:07

Question Overflow


People also ask

How to implement MySQL case with or condition?

How to implement MySQL CASE with OR condition? To understand the above syntax, let us create a table. The query to create a table is as follows Insert some records in the table using insert command. Display all records from the table using select statement.

How do I use the and condition and or condition in MySQL?

This MySQL tutorial explains how to use the AND condition and the OR condition together in a MySQL query with syntax and examples. The MySQL AND condition and OR condition can be combined in a SELECT, INSERT, UPDATE, or DELETE statement.

How do I use different conditions in the case statement?

Here are two different ways how you can use different conditions in the CASE statement. A simple CASE expression checks one expression against multiple values. Within a SELECT statement, a simple CASE expression allows only an equality check; no other comparisons are made.

What is a simple CASE statement in SQL?

A simple CASE expression checks one expression against multiple values. Within a SELECT statement, a simple CASE expression allows only an equality check; no other comparisons are made. A simple CASE expression operates by comparing the first expression to the expression in each WHEN clause for equivalence.


1 Answers

CASE ranking
  WHEN 1 THEN 'alpha' 
  WHEN 2 THEN 'beta'  
  WHEN 10 THEN 'charlie' 
  ELSE 'zelta' 
END CASE;

You can use one of expresions that WHEN has, but you cannot mix both of them.

1) WHEN when_expression Is a simple expression to which input_expression is compared when the simple CASE format is used. when_expression is any valid expression. The data types of input_expression and each when_expression must be the same or must be an implicit conversion.

2) WHEN Boolean_expression Is the Boolean expression evaluated when using the searched CASE format. Boolean_expression is any valid Boolean expression.

You could program:

1)

 CASE ProductLine
         WHEN 'R' THEN 'Road'
         WHEN 'M' THEN 'Mountain'
         WHEN 'T' THEN 'Touring'
         WHEN 'S' THEN 'Other sale items'
         ELSE 'Not for sale'

2)

CASE 
         WHEN ListPrice =  0 THEN 'Mfg item - not for resale'
         WHEN ListPrice < 50 THEN 'Under $50'
         WHEN ListPrice >= 50 and ListPrice < 250 THEN 'Under $250'
         WHEN ListPrice >= 250 and ListPrice < 1000 THEN 'Under $1000'
         ELSE 'Over $1000'
      END

But in any case you can expect that the variable ranking is going to be compared in a boolean expresion.

http://msdn.microsoft.com/en-us/library/ms181765.aspx

like image 69
Alfaplus Avatar answered Oct 28 '22 17:10

Alfaplus