SQL CASE Statement
Giả sử ta có bảng sau :)))
mysql> SELECT customerNumber, customerName FROM customers;
+----------------+------------------------------------+
| customerNumber | customerName |
+----------------+------------------------------------+
| 462 | FunGiftIdeas.com |
| 465 | Anton Designs, Ltd. |
| 471 | Australian Collectables, Ltd |
| 473 | Frau da Collezione |
| 475 | West Coast Collectables Co. |
| 477 | Mit Vergnügen & Co. |
| 480 | Kremlin Collectables, Co. |
| 481 | Raanan Stores, Inc |
| 484 | Iberia Gift Imports, Corp. |
| 486 | Motor Mint Distributors Inc. |
| 487 | Signal Collectibles Ltd. |
| 489 | Double Decker Gift Stores, Ltd |
| 495 | Diecast Collectables |
| 496 | Kelly's Gift Shop |
+----------------+------------------------------------+
Giờ ta sử dụng câu lệnh sau để sử dụng lẹnh CASE cho cột customerNumber
mysql> SELECT customerNumber, customerName, CASE
-> WHEN customerNumber > 460 && customerNumber < 470 THEN ">460 && < 470"
-> WHEN customerNumber = 470 THEN "470"
-> WHEN customerNumber > 470 THEN ">470"
-> END AS custom
-> FROM customers;
Kết quả:
+----------------+------------------------------------+---------------+
| customerNumber | customerName | custom |
+----------------+------------------------------------+---------------+
| 462 | FunGiftIdeas.com | >460 && < 470 |
| 465 | Anton Designs, Ltd. | >460 && < 470 |
| 471 | Australian Collectables, Ltd | >470 |
| 473 | Frau da Collezione | >470 |
| 475 | West Coast Collectables Co. | >470 |
| 477 | Mit Vergnügen & Co. | >470 |
| 480 | Kremlin Collectables, Co. | >470 |
| 481 | Raanan Stores, Inc | >470 |
| 484 | Iberia Gift Imports, Corp. | >470 |
| 486 | Motor Mint Distributors Inc. | >470 |
| 487 | Signal Collectibles Ltd. | >470 |
| 489 | Double Decker Gift Stores, Ltd | >470 |
| 495 | Diecast Collectables | >470 |
| 496 | Kelly's Gift Shop | >470 |
+----------------+------------------------------------+---------------+
Last updated