MySQL多表查询讲解( 五 )

select NULL 返回了列,所以:
mysql> select NULL;+------+| NULL |+------+| NULL |+------+1 row in set (0.00 sec) mysql> select * from employee where exists (select NULL) and salary > 10000;+----+-----------+-----+-----+----------+------------+---------+| id | name| age | sex | salary| hire_date| dept_id |+----+-----------+-----+-----+----------+------------+---------+|2 | 奥力给 |30 |1 | 18000.00 | 2020-01-08 |1 |+----+-----------+-----+-----+----------+------------+---------+1 row in set (0.00 sec)ot exists与exists相反 。
8 判断查询8.1 IF语法:
if(条件表达式,"结果为true","结果为false");举例,将薪资大于大于8000的员工薪资级别设置为小康,小于8000设置为一般:
select *,if(salary > 8000,"小康","一般") as salary_level from employee;输出结果:
mysql> select *,if(salary > 8000,"小康","一般") as salary_level from employee;+----+-----------+-----+-----+----------+------------+---------+--------------+| id | name| age | sex | salary| hire_date| dept_id | salary_level |+----+-----------+-----+-----+----------+------------+---------+--------------+|1 | 菜虚鲲 |20 |2 | 10000.00 | 2020-01-10 |1 | 小康||2 | 奥力给 |30 |1 | 18000.00 | 2020-01-08 |1 | 小康||3 | 老八|28 |1 | 7000.00| 2020-01-07 |1 | 一般||4 | 小张|25 |1 | 8000.00| 2020-01-10 |1 | 一般||5 | 小红|20 |2 | 6000.00| 2020-01-05 |2 | 一般||6 | 小丽|23 |2 | 6500.00| 2020-01-05 |2 | 一般||7 | 小花|21 |2 | 5500.00| 2020-01-10 |2 | 一般||8 | 马小跳 |25 |1 | 7000.00| 2020-01-01 |3 | 一般||9 | 张大骚 |30 |1 | 9000.00| 2020-01-07 |3 | 小康|| 10 | 马冬梅 |31 |2 | 5000.00| 2020-01-07 |4 | 一般|| 11 | 川坚果 |60 |1 | 100.00| 2020-01-08 | NULL| 一般|+----+-----------+-----+-----+----------+------------+---------+--------------+11 rows in set (0.00 sec)8.2 if … else if … else 形式语法:
select ...,case when [条件1] then [result]when [条件2] then [result]else [result]endfrom tableName;举例:
根据员工工资划分员工生活水平,小于7000为贫穷,7000 到 9000为一般,9000-10000为中等,10000-12000为中等偏上,大于12000为有钱 。
mysql> select name,salary,casewhen salary < 7000 then '贫穷'->when salary < 9000 then '一般'->when salary < 10000 then '中等'->when salary < 12000 then '中等偏上'->else '有钱'->end as living_standard-> from employee;+-----------+----------+-----------------+| name| salary| living_standard |+-----------+----------+-----------------+| 菜虚鲲 | 10000.00 | 中等偏上|| 奥力给 | 18000.00 | 有钱|| 老八| 7000.00| 一般|| 小张| 8000.00| 一般|| 小红| 6000.00| 贫穷|| 小丽| 6500.00| 贫穷|| 小花| 5500.00| 贫穷|| 马小跳 | 7000.00| 一般|| 张大骚 | 9000.00| 中等|| 马冬梅 | 5000.00| 贫穷|| 川坚果 | 100.00| 贫穷|+-----------+----------+-----------------+11 rows in set (0.00 sec)其形式类似于if…else if…else if…else形式 。
8.3 switch case形式语法:
select ..., case s1when [value1] then [result1]when [value2] then [result2]when [value3] then [result3]else [resultOther] endfrom tableName;举例,直接显示出员工性别:
mysql> select *,case sex->when 1 then '男'->when 2 then '女'->else '未知' end as employee_sex-> from employee;+----+-----------+-----+-----+----------+------------+---------+--------------+| id | name| age | sex | salary| hire_date| dept_id | employee_sex |+----+-----------+-----+-----+----------+------------+---------+--------------+|1 | 菜虚鲲 |20 |2 | 10000.00 | 2020-01-10 |1 | 女||2 | 奥力给 |30 |1 | 18000.00 | 2020-01-08 |1 | 男||3 | 老八|28 |1 | 7000.00| 2020-01-07 |1 | 男||4 | 小张|25 |1 | 8000.00| 2020-01-10 |1 | 男||5 | 小红|20 |2 | 6000.00| 2020-01-05 |2 | 女||6 | 小丽|23 |2 | 6500.00| 2020-01-05 |2 | 女||7 | 小花|21 |2 | 5500.00| 2020-01-10 |2 | 女||8 | 马小跳 |25 |1 | 7000.00| 2020-01-01 |3 | 男||9 | 张大骚 |30 |1 | 9000.00| 2020-01-07 |3 | 男|| 10 | 马冬梅 |31 |2 | 5000.00| 2020-01-07 |4 | 女|| 11 | 川坚果 |60 |1 | 100.00| 2020-01-08 | NULL| 男|+----+-----------+-----+-----+----------+------------+---------+--------------+11 rows in set (0.00 sec)
【MySQL多表查询讲解】


推荐阅读