MySQL查询由于IF语句返回一个字符串?
让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, EmployeeSalary int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(EmployeeSalary) values(12000); mysql> insert into DemoTable(EmployeeSalary) values(20000); mysql> insert into DemoTable(EmployeeSalary) values(11500); mysql> insert into DemoTable(EmployeeSalary) values(15500);
使用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)