[英]MySQL COUNT with GROUP BY and zero entries
I have 2 tables: 我有2张桌子:
Table 1. options_ethnicity
with the following entries: 表1.
options_ethnicity
,包含以下条目:
ethnicity_id ethnicity_name
1 White
2 Hispanic
3 African/American
Table 2. inquiries
with the following entries: 表2.具有以下条目的
inquiries
:
inquiry_id ethnicity_id
1 1
2 1
3 1
4 2
5 2
I want to generate a table that shows the number of inquires by ethnicity. 我想生成一个表格,显示按种族划分的查询次数。 My query so far looks like this:
到目前为止,我的查询如下所示:
SELECT options_ethnicity.ethnicity_name, COUNT('inquiries.ethnicity_id') AS count
FROM (inquiries
LEFT JOIN options_ethnicity ON
options_ethnicity.ethnicity_id = inquiries.ethnicity_id)
GROUP BY options_ethnicity.ethnicity_id
The query gives the correct answer but there is no column for African/American which has 0 results. 该查询给出了正确的答案,但没有非洲/美国的列有0个结果。
White 3
Hispanic 2
If I replace the LEFT JOIN with a RIGHT JOIN, I get all 3 ethnicity names, but the count for African/American is wrong. 如果我用右连接替换LEFT JOIN,我会获得所有3个种族名称,但非洲/美国人的数量是错误的。
White 3
Hispanic 2
African/American 1
Any help would be appreciated. 任何帮助,将不胜感激。
Here's an update to this post with what seems to be a working query: 这是对这篇文章的更新,看起来似乎是一个有效的查询:
SELECT
options_ethnicity.ethnicity_name,
COALESCE(COUNT(inquiries.ethnicity_id), 0) AS count
FROM options_ethnicity LEFT JOIN inquiries ON inquiries.ethnicity_id = options_ethnicity.ethnicity_id
GROUP BY options_ethnicity.ethnicity_id
UNION ALL
SELECT
'NULL Placeholder' AS ethnicity_name,
COUNT(inquiries.inquiry_id) AS count
FROM inquiries
WHERE inquiries.ethnicity_id IS NULL
Because you're using a LEFT JOIN, references to the table defined in the LEFT JOIN can be null. 因为您正在使用LEFT JOIN,所以对LEFT JOIN中定义的表的引用可以为null。 Which means you need to convert this NULL value to zero (in this case):
这意味着您需要将此NULL值转换为零(在本例中):
SELECT oe.ethnicity_name,
COALESCE(COUNT(i.ethnicity_id), 0) AS count
FROM OPTIONS_ETHNICITY oe
LEFT JOIN INQUIRIES i ON i.ethnicity_id = oe.ethnicity_id
GROUP BY oe.ethnicity_id
This example uses COALESCE , an ANSI standard means of handling NULL values. 此示例使用COALESCE ,ANSI标准处理NULL值的方法。 It will return the first non-null value, but if none can be found it will return null.
它将返回第一个非null值,但如果找不到任何值,则返回null。 IFNULL is a valid alternative on MySQL, but it is not portable to other databases while COALESCE is.
IFNULL是MySQL的有效替代品,但它不能移植到其他数据库,而COALESCE则是。
In the real database table, there are some entries in the inquires table where the ethnicity_id is NULL, ie the ethnicity was not recorded.
在真实数据库表中,在查询表中有一些条目,其中ethnicity_id为NULL,即没有记录种族。 Any idea on how to get these null values to be counted so that they can be shown?
有关如何计算这些空值以便显示它们的任何想法?
I think I understand the issue you're facing: 我想我理解你所面临的问题:
SELECT oe.ethnicity_name,
COALESCE(COUNT(i.ethnicity_id), 0) AS count
FROM (SELECT t.ethnicity_name,
t.ethnicity_id
FROM OPTIONS_ETHNICITY t
UNION ALL
SELECT 'NULL placeholder' AS ethnicity_name,
NULL AS ethnicity_id) oe
LEFT JOIN INQUIRIES i ON i.ethnicity_id = oe.ethnicity_id
GROUP BY oe.ethnicity_id
This will pickup all the NULL ethncity_id instances, but it will attribute the counting to the "NULL placeholder" group. 这将拾取所有NULL ethncity_id实例,但它会将计数归因于“NULL占位符”组。 IE:
IE:
ethnicity_name | COUNT
------------------------
White | 3
Hispanic | 2
NULL placeholder | ?
You counted a string instead of the right column 您计算了一个字符串而不是右列
SELECT options_ethnicity.ethnicity_name, COUNT(inquiries.ethnicity_id) AS count
FROM inquiries
RIGHT JOIN options_ethnicity ON options_ethnicity.ethnicity_id = inquiries.ethnicity_id
GROUP BY options_ethnicity.ethnicity_id
Why don't you "reverse" your query? 为什么不“反转”你的查询?
SELECT
options_ethnicity.ethnicity_name,
COUNT(inquiries.ethnicity_id) AS count
FROM
options_ethnicity
Left Join inquiries On options_ethnicity.ethnicity_id = inquiries.ethnicity_id
GROUP BY
options_ethnicity.ethnicity_id
You still might need a Coalesce call, but to me, this query makes more sense for what you're trying to accomplish. 你仍然可能需要一个Coalesce调用,但对我来说,这个查询对你想要完成的事情更有意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.