您可以将SELECT IF语句与OR一起使用。要了解OR的选择,让我们创建一个表。创建表的查询如下-
mysql> create table EmployeeInformation -> ( -> EmployeeId int, -> EmployeeName varchar(100), -> EmployeeStatus varchar(100) -> );
使用insert命令在表中插入一些记录。查询如下-
mysql> insert into EmployeeInformation values(1,'Sam','FullTime'); mysql> insert into EmployeeInformation values(2,'Mike','PartTime'); mysql> insert into EmployeeInformation values(3,'Bob','Intern'); mysql> insert into EmployeeInformation values(4,'Carol','FullTime'); mysql> insert into EmployeeInformation values(5,'John','FullTime'); mysql> insert into EmployeeInformation values(6,'Johnson','PartTime'); mysql> insert into EmployeeInformation values(7,'Maria','Intern');
现在让我们使用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语句的查询。在下面的查询中,您将仅获得具有EmployeeStatus 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)