如何在 MySQL CASE 语句中使用 OR 条件?
以下是 MySQL CASE OR 条件的语法
SELECT yourColumnName1,.....N , CASE WHEN yourColumnName2=0 or yourColumnName2IS NULL THEN 'yourMessage1' ELSE 'yourMessage2' END AS yourAliasName FROM yourTableName;
为了理解以上语法,我们创建一个表。创建表的查询如下
mysql> create table ReservationSystems - > ( - > Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, - > Name varchar(20), - > isSeatConfirmed tinyint - > ); Query OK, 0 rows affected (0.61 sec)
使用 insert 命令向表中插入一些记录
查询如下
mysql> insert into ReservationSystems(Name,isSeatConfirmed) values('Larry',0); Query OK, 1 row affected (0.12 sec) mysql> insert into ReservationSystems(Name,isSeatConfirmed) values('Bob',1); Query OK, 1 row affected (0.15 sec) mysql> insert into ReservationSystems(Name,isSeatConfirmed) values('Mike',NULL); Query OK, 1 row affected (0.14 sec) mysql> insert into ReservationSystems(Name,isSeatConfirmed) values('David',1); Query OK, 1 row affected (0.17 sec) mysql> insert into ReservationSystems(Name,isSeatConfirmed) values('Carol',0); Query OK, 1 row affected (0.19 sec) mysql> insert into ReservationSystems(Name,isSeatConfirmed) values('James',1); Query OK, 1 row affected (0.11 sec)
使用 select 语句显示表中的所有记录
查询如下
mysql> select *from ReservationSystems;
输出如下
+----+-------+-----------------+ | Id | Name | isSeatConfirmed | +----+-------+-----------------+ | 1 | Larry | 0 | | 2 | Bob | 1 | | 3 | Mike | NULL | | 4 | David | 1 | | 5 | Carol | 0 | | 6 | James | 1 | +----+-------+-----------------+ 6 rows in set (0.00 sec)
以下是在 MySQL CASE 中使用 OR 条件
mysql> select Name, - > CASE WHEN isSeatConfirmed=0 or isSeatConfirmed IS NULL THEN 'YOUR SEAT IS NOT CONFIRMED' - > ELSE 'YOUR SEAT IS CONFIRMED' - > END AS SEATCONFIRMSTATUS - > FROM ReservationSystems;
输出如下
+-------+----------------------------+ | Name | SEATCONFIRMSTATUS | +-------+----------------------------+ | Larry | YOUR SEAT IS NOT CONFIRMED | | Bob | YOUR SEAT IS CONFIRMED | | Mike | YOUR SEAT IS NOT CONFIRMED | | David | YOUR SEAT IS CONFIRMED | | Carol | YOUR SEAT IS NOT CONFIRMED | | James | YOUR SEAT IS CONFIRMED | +-------+----------------------------+ 6 rows in set (0.00 sec)
广告