MySQL SELECT IF 语句与 OR 一起使用?
mysqlmysqli database
您可以将 SELECT IF 语句与 OR 一起使用。为了理解使用 OR 进行选择,让我们创建一个表。创建表的查询如下 −
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 语句的查询。在下面的查询中,您将仅获得具有 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)