显示带条件的查询mysql

thtygnil  于 2021-08-04  发布在  Java
关注(0)|答案(1)|浏览(277)

我的员工表如下:

+-------------+---------+------------------+
| employee_id |  phone  |       dept       |
+-------------+---------+------------------+
|           1 | 7738474 | marketing        |
|           2 | [null]  | marketing        |
|           3 | 9938475 | customer service |
|           4 | 4233474 | customer service |
|           5 | 1738477 | marketing        |
|           6 | [null]  | marketing        |
|           7 | [null]  | customer service |
+-------------+---------+------------------+

我想如果市场部有空电话值,他们不显示,如果客户服务部,他们显示所有的价值,即使电话是空的

+-------------+---------+------------------+
| employee_id |  phone  |       dept       |
+-------------+---------+------------------+
|           1 | 7738474 | marketing        |
|           3 | 9938475 | customer service |
|           4 | 4233474 | customer service |
|           5 | 1738477 | marketing        |
|           7 | [null]  | customer service |
+-------------+---------+------------------+

有没有不使用union的查询?

select * from employee where dept ='customer service'
union ALL 
select * from employee where dept = 'marketing' and phone is not null
iaqfqrcu

iaqfqrcu1#

这听起来像是相对简单的过滤:

select *
from employee e
where dept = 'customer service' or
      (dept = 'marketing' and phone is not null)

相关问题