这是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 - > );
使用insert命令在表中插入一些记录。
查询如下
mysql> insert into ReservationSystems(Name,isSeatConfirmed) values('Larry',0); mysql> insert into ReservationSystems(Name,isSeatConfirmed) values('Bob',1); mysql> insert into ReservationSystems(Name,isSeatConfirmed) values('Mike',NULL); mysql> insert into ReservationSystems(Name,isSeatConfirmed) values('David',1); mysql> insert into ReservationSystems(Name,isSeatConfirmed) values('Carol',0); mysql> insert into ReservationSystems(Name,isSeatConfirmed) values('James',1);
使用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)
这是带有OR条件的MySQL CASE
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)