MySQL 中 COUNT 的默认值

发布于 2024-08-06 14:22:17 字数 706 浏览 6 评论 0原文

我有一个看起来像这样的表:

+-------+-----+
|country|prop1|
+-------+-----+
|RO     |  1  |
|RO     |  2  |
|UK     |  1  |
|IT     |  2  |
+-------+-----+

我想计算 prop1 不为空的行,我使用以下选择:

SELECT `country`, COUNT(*) as number FROM table GROUP BY `country`;

这将返回:

+-------+------+
|country|number|
+-------+------+
|RO     |  2   |
|UK     |  1   |
|IT     |  1   |
+-------+------+

但是我需要以下内容:

+-------+------+
|country|number|
+-------+------+
|RO     |  2   |
|UK     |  1   |
|IT     |  1   |
|FR     |  0   |
+-------+------+

你认为这样的东西可以写吗直接在SQL中?我在想类似指定“国家”的可能值列表和默认值(0)(如果在表中找不到)。

I have a table looking something like this:

+-------+-----+
|country|prop1|
+-------+-----+
|RO     |  1  |
|RO     |  2  |
|UK     |  1  |
|IT     |  2  |
+-------+-----+

I want to count the rows for which the prop1 is not null and I use the following select:

SELECT `country`, COUNT(*) as number FROM table GROUP BY `country`;

this will return:

+-------+------+
|country|number|
+-------+------+
|RO     |  2   |
|UK     |  1   |
|IT     |  1   |
+-------+------+

however I need the following:

+-------+------+
|country|number|
+-------+------+
|RO     |  2   |
|UK     |  1   |
|IT     |  1   |
|FR     |  0   |
+-------+------+

Do you think something like this can be possible to write directly in SQL? I was thinking something like specifying list of possible values for "country" and a default value (0) if it is not found in the table.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

去了角落 2024-08-13 14:22:17

在您的示例中, FR 来自何处并不明显。

MySQL 内部没有国家/地区列表,因此应从某处获取国家/地区代码。

如果您在 mytable 中包含所有国家/地区(prop 可能设置为 NULL):

SELECT  country, COUNT(prop) as number
FROM    mytable
GROUP BY
        country

如果您在单独的表中包含国家/地区(并且一个国家/地区可能会mytable 中缺失):

SELECT  c.id, COUNT(m.prop) as number
FROM    countries c
LEFT JOIN
        mytable m
ON      m.country = c.id
GROUP BY
        c.id

It's not obvious in your example where the FR comes from.

MySQL does not have a list of countries inside it, so country codes should be taken from somewhere.

If you have all countries inside mytable (with prop possibly set to NULL):

SELECT  country, COUNT(prop) as number
FROM    mytable
GROUP BY
        country

If you have countries in a separate table (and a country may be missing in mytable):

SELECT  c.id, COUNT(m.prop) as number
FROM    countries c
LEFT JOIN
        mytable m
ON      m.country = c.id
GROUP BY
        c.id
怀里藏娇 2024-08-13 14:22:17

我认为你必须与所有国家建立一张桌子。最好更改当前的表以使用国家/地区的键。这样您就可以进行 LEFT JOIN。这将为您提供所有国家/地区,如果该国家/地区没有道具,则值为 NULL。

I think you will have to set up a table with all the countries. It will be best to alter your current table to use the keys of the countries. So that you can do a LEFT JOIN. That will give you all the countries, with NULL values if there are no props for that country.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文