我有一个名为employees
的表,其架构如下所示:
mysql> desc employees;
+------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+-------+
| emp_no | int(11) | NO | PRI | NULL | |
| birth_date | date | NO | | NULL | |
| first_name | varchar(14) | NO | | NULL | |
| last_name | varchar(16) | NO | | NULL | |
| gender | enum('M','F') | NO | | NULL | |
| hire_date | date | NO | | NULL | |
+------------+---------------+------+-----+---------+-------+
我只想查询男性(emp_no
和女性(where generder = 'M')
)的where generder = 'F'
计数。
我想要的结果如下:
+--------+--------+
| Male | FeMale |
+--------+--------+
| 300 | 120 |
+--------+--------+
有关如何在单个查询中执行此操作的任何建议吗?
答案 0 :(得分:1)
SELECT SUM(gender = 'M') AS Male, SUM(gender = 'F') AS Female
FROM employees
答案 1 :(得分:0)
select sum(case when gender = 'm' then 1 else 0 end) male,
sum(case when gender = 'f' then 1 else 0 end) female,
from employees
你可以尝试类似的东西