id我有一张这样的桌子
|----|-----------|--------|
| ID | Ethnicity | Gender |
| 1 | 2 | 1 |
| 2 | 3 | 1 |
| 3 | 4 | 2 |
|----|-----------|--------|
etc ....
我正在尝试取回一组结果,这些结果按男性(1)和女性(2)显示我的种族
因此,此示例中的结果行将是:
Ethnicity Male Female
2 1 0
3 1 0
4 0 1
到目前为止,我已经接近我想要的:
SELECT ethnicity,
(SELECT
count(id)
FROM
table_name
WHERE
gender = '2' ) as female,
(SELECT
count(id)
FROM
table_name
WHERE
gender = '1') as male
FROM table_name
GROUP BY ethnicity
这给了我:
Ethnicity Male Female
2 2 1
3 2 1
4 2 1
但是count(id)
,如果有意义的话,只需要计算该种族行的adno。