在 MySQL CASE 表示式中如何使用“OR”條件?\n
在 MySQL CASE 表示式中設定一個類似於“OR”的條件。我們首先建立一個示例表。
以下是查詢
mysql> create table caseOrConditionDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(100), -> Score int -> ); Query OK, 0 rows affected (0.49 sec)
以下是使用 insert 命令在表中插入一些記錄的查詢
mysql> insert into caseOrConditionDemo(Name,Score) values('Larry',85);
Query OK, 1 row affected (0.18 sec)
mysql> insert into caseOrConditionDemo(Name,Score) values('Sam',74);
Query OK, 1 row affected (0.20 sec)
mysql> insert into caseOrConditionDemo(Name,Score) values('Mike',76);
Query OK, 1 row affected (0.16 sec)
mysql> insert into caseOrConditionDemo(Name,Score) values('Carol',65);
Query OK, 1 row affected (0.20 sec)以下是使用 select 命令從表中顯示記錄的查詢
mysql> select *from caseOrConditionDemo;
這將產生以下輸出
+----+-------+-------+ | Id | Name | Score | +----+-------+-------+ | 1 | Larry | 85 | | 2 | Sam | 74 | | 3 | Mike | 76 | | 4 | Carol | 65 | +----+-------+-------+ 4 rows in set (0.00 sec)
以下是使用 MySQL CASE 表示式的一個類似於“OR”的條件的查詢
mysql> select Id,Name,Score, -> case when Score > 75 then 'Better Score' -> when Score > 70 then 'Good Score' -> else 'Not Good Score' -> end as 'Performance' -> from caseOrConditionDemo;
這將產生以下輸出
+----+-------+-------+----------------+ | Id | Name | Score | Performance | +----+-------+-------+----------------+ | 1 | Larry | 85 | Better Score | | 2 | Sam | 74 | Good Score | | 3 | Mike | 76 | Better Score | | 4 | Carol | 65 | Not Good Score | +----+-------+-------+----------------+ 4 rows in set (0.04 sec)
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
安卓
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP