martes, 29 de octubre de 2024

MYSQL CASE

 mysql> select * from orders;

+----------+-----------+------------+------------+----------+

| order_id | client_id | product_id | order_date | quantity |

+----------+-----------+------------+------------+----------+

|        1 |         1 |          1 | 2023-05-01 |        2 |

|        2 |         1 |          2 | 2023-05-03 |        1 |

|        4 |         3 |          1 | 2023-07-20 |        1 |

+----------+-----------+------------+------------+----------+

3 rows in set (0.00 sec)


mysql> 

mysql> 

mysql> 

mysql> SELECT 

    ->     order_id,

    ->     client_id,

    ->     product_id,

    ->     order_date,

    ->     quantity,

    ->     CASE 

    ->         WHEN quantity = 1 THEN 'Single'

    ->         WHEN quantity BETWEEN 2 AND 5 THEN 'Small Order'

    ->         WHEN quantity BETWEEN 6 AND 10 THEN 'Medium Order'

    ->         ELSE 'Large Order'

    ->     END AS order_category

    -> FROM 

    ->     orders;

+----------+-----------+------------+------------+----------+----------------+

| order_id | client_id | product_id | order_date | quantity | order_category |

+----------+-----------+------------+------------+----------+----------------+

|        1 |         1 |          1 | 2023-05-01 |        2 | Small Order    |

|        2 |         1 |          2 | 2023-05-03 |        1 | Single         |

|        4 |         3 |          1 | 2023-07-20 |        1 | Single         |

+----------+-----------+------------+------------+----------+----------------+

3 rows in set (0.00 sec)


mysql> 


No hay comentarios:

Publicar un comentario