# 3.1. CASE

**CASE** thực hiện việc thêm giá trị cho một cột mới dựa trên điều kiện.

**<span style="mso-no-proof: yes;"><span style="mso-list: Ignore;">a)<span style="font-style: normal; font-variant: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 7pt; line-height: normal; font-family: 'Times New Roman';"> </span></span></span>Cú pháp:**

![](https://book.inetcloud.vn/uploads/images/gallery/2024-07/embedded-image-eeuimvna.png)

<span style="font-size: 12.0pt; mso-fareast-font-family: 'Times New Roman'; mso-no-proof: yes;"> </span>

**<span style="mso-list: Ignore;">b)<span style="font-style: normal; font-variant: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 7pt; line-height: normal; font-family: 'Times New Roman';"> </span></span>Ví dụ:**

Thêm giá trị theo điều kiện của cột i:

```mysql
-- i[1,2,3]
SELECT i, CASE WHEN i > 2 THEN 1 ELSE 0 END AS test
FROM integers;

```

<table border="1" cellpadding="0" cellspacing="0" id="bkmrk-i-test-1-0-2-0-3-1-1" style="border-collapse: collapse; width: 96pt; border-spacing: 1px; border: 0px groove rgb(0, 0, 0);" width="128"><colgroup><col span="2" style="width: 48pt;" width="64"></col></colgroup><thead><tr style="height: 15.0pt; mso-yfti-firstrow: yes; mso-yfti-irow: 0;"><td class="xl63" height="20" style="height: 15pt; width: 48pt; padding: 1px; border-color: rgb(0, 0, 0); background-color: rgb(217, 217, 217);" width="64">**<span style="mso-color-alt: windowtext;">i</span>**</td><td class="xl64" style="width: 48pt; padding: 1px; border-color: rgb(0, 0, 0); background-color: rgb(217, 217, 217);" width="64">**<span style="mso-color-alt: windowtext;">test</span>**</td></tr></thead><tbody><tr style="height: 15.0pt; mso-yfti-irow: 1;"><td class="xl65 align-left" height="20" style="height: 15pt; width: 48pt; padding: 1px; border-color: rgb(0, 0, 0);" width="64">1</td><td class="xl66 align-left" style="width: 48pt; padding: 1px; border-color: rgb(0, 0, 0);" width="64">0</td></tr><tr style="height: 15.0pt; mso-yfti-irow: 2;"><td class="xl65 align-left" height="20" style="height: 15pt; width: 48pt; padding: 1px; border-color: rgb(0, 0, 0);" width="64">2</td><td class="xl66 align-left" style="width: 48pt; padding: 1px; border-color: rgb(0, 0, 0);" width="64">0</td></tr><tr style="height: 15.0pt; mso-yfti-irow: 3; mso-yfti-lastrow: yes;"><td class="xl65 align-left" height="20" style="height: 15pt; width: 48pt; padding: 1px; border-color: rgb(0, 0, 0);" width="64">3</td><td class="xl66 align-left" style="width: 48pt; padding: 1px; border-color: rgb(0, 0, 0);" width="64">1</td></tr></tbody></table>

Phần **WHEN<span style="mso-spacerun: yes;"> </span>THEN** của biểu thức điều kiện **CASE** có thể được nối tiếp, bất cứ khi nào bất kỳ điều kiện nào trả về giá trị true cho một bộ đơn lẻ, biểu thức tương ứng sẽ được đánh giá và trả về.

```mysql
SELECT i, CASE WHEN i = 1 THEN 10 WHEN i = 2 THEN 20 ELSE 0 END AS test
FROM integers;

```

<table border="1" cellpadding="0" cellspacing="0" id="bkmrk-i-test-1-10-2-20-3-0" style="border-collapse: collapse; width: 96px; border-spacing: 1px; border: 0px groove rgb(0, 0, 0);" width="128"><colgroup><col span="2" style="width: 48pt;" width="64"></col></colgroup><thead><tr style="height: 15.0pt; mso-yfti-firstrow: yes; mso-yfti-irow: 0;"><td class="xl63" height="20" style="height: 15pt; width: 48pt; padding: 1px; border-color: rgb(0, 0, 0); background-color: rgb(217, 217, 217);" width="64"><span style="mso-color-alt: windowtext;">i</span></td><td class="xl64" style="width: 48pt; padding: 1px; border-color: rgb(0, 0, 0); background-color: rgb(217, 217, 217);" width="64"><span style="mso-color-alt: windowtext;">test</span></td></tr></thead><tbody><tr style="height: 15.0pt; mso-yfti-irow: 1;"><td class="xl65 align-left" height="20" style="height: 15pt; width: 48pt; padding: 1px; border-color: rgb(0, 0, 0);" width="64">1</td><td class="xl66 align-left" style="width: 48pt; padding: 1px; border-color: rgb(0, 0, 0);" width="64">10</td></tr><tr style="height: 15.0pt; mso-yfti-irow: 2;"><td class="xl65 align-left" height="20" style="height: 15pt; width: 48pt; padding: 1px; border-color: rgb(0, 0, 0);" width="64">2</td><td class="xl66 align-left" style="width: 48pt; padding: 1px; border-color: rgb(0, 0, 0);" width="64">20</td></tr><tr style="height: 15.0pt; mso-yfti-irow: 3; mso-yfti-lastrow: yes;"><td class="xl65 align-left" height="20" style="height: 15pt; width: 48pt; padding: 1px; border-color: rgb(0, 0, 0);" width="64">3</td><td class="xl66 align-left" style="width: 48pt; padding: 1px; border-color: rgb(0, 0, 0);" width="64">0</td></tr></tbody></table>