带 OR 的 MySQL SELECT IF 语句?
你可以将 SELECT IF 语句与 OR 一起使用。要了解带有 OR 的 select 语句,让我们创建一个表。创建表的查询如下 −
mysql> create table EmployeeInformation -> ( -> EmployeeId int, -> EmployeeName varchar(100), -> EmployeeStatus varchar(100) -> ); Query OK, 0 rows affected (0.68 sec)
使用 insert 命令向表中插入一些记录。查询如下 −
mysql> insert into EmployeeInformation values(1,'Sam','FullTime'); Query OK, 1 row affected (0.23 sec) mysql> insert into EmployeeInformation values(2,'Mike','PartTime'); Query OK, 1 row affected (0.14 sec) mysql> insert into EmployeeInformation values(3,'Bob','Intern'); Query OK, 1 row affected (0.14 sec) mysql> insert into EmployeeInformation values(4,'Carol','FullTime'); Query OK, 1 row affected (0.16 sec) mysql> insert into EmployeeInformation values(5,'John','FullTime'); Query OK, 1 row affected (0.19 sec) mysql> insert into EmployeeInformation values(6,'Johnson','PartTime'); Query OK, 1 row affected (0.19 sec) mysql> insert into EmployeeInformation values(7,'Maria','Intern'); Query OK, 1 row affected (0.12 sec)
现在让我们使用 select 命令显示表中的所有记录。查询如下 −
mysql> select *from EmployeeInformation;
输出
+------------+--------------+----------------+ | EmployeeId | EmployeeName | EmployeeStatus | +------------+--------------+----------------+ | 1 | Sam | FullTime | | 2 | Mike | PartTime | | 3 | Bob | Intern | | 4 | Carol | FullTime | | 5 | John | FullTime | | 6 | Johnson | PartTime | | 7 | Maria | Intern | +------------+--------------+----------------+ 7 rows in set (0.00 sec)
以下是执行带有 OR 的 SELECT IF 语句的查询。在下面的查询中,你将只获得带有员工状态 FullTime 和 Intern 的 EmployeeName,否则你将获得员工的状态。
查询如下 −
mysql> select if(EmployeeStatus='FullTime' or EmployeeStatus='Intern',EmployeeName,EmployeeStatus) as Status from EmployeeInformation;
输出
+----------+ | Status | +----------+ | Sam | | PartTime | | Bob | | Carol | | John | | PartTime | | Maria | +----------+ 7 rows in set (0.00 sec)
广告