将字符串作为 IF 语句结果返回的 MySQL 查询?
我们首先创建一个表格 -
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, EmployeeSalary int ); Query OK, 0 rows affected (1.68 sec)
使用插入命令在表格中插入一些记录 -
mysql> insert into DemoTable(EmployeeSalary) values(12000); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable(EmployeeSalary) values(20000); Query OK, 1 row affected (0.55 sec) mysql> insert into DemoTable(EmployeeSalary) values(11500); Query OK, 1 row affected (0.94 sec) mysql> insert into DemoTable(EmployeeSalary) values(15500); Query OK, 1 row affected (0.44 sec)
使用 select 语句从表格中显示所有记录 -
mysql> select *from DemoTable;
这将生成以下输出 -
+----+----------------+ | Id | EmployeeSalary | +----+----------------+ | 1 | 12000 | | 2 | 20000 | | 3 | 11500 | | 4 | 15500 | +----+----------------+ 4 rows in set (0.00 sec)
以下是查询,它将字符串作为 IF 语句的结果返回 -
mysql> select Id,if(EmployeeSalary <= 12000 ,'Internship Employee','FullTime Employee') as status from DemoTable;
这将生成以下输出 -
+----+---------------------+ | Id | status | +----+---------------------+ | 1 | Internship Employee | | 2 | FullTime Employee | | 3 | Internship Employee | | 4 | FullTime Employee | +----+---------------------+ 4 rows in set (0.00 sec)
广告