sql语法、计数、分组依据和添加语句

发布于 2024-12-21 10:22:37 字数 619 浏览 1 评论 0原文

我有一个像这样的sql语句(使用mysql 5.5.15):

select global_id, count(id) as group_count
from globals_lists
group by global_id
order by group_count desc;

并且想修改它以支持带有这样的in语句的子集:

select global_id, count(id) as group_count
from globals_lists
where global_id in (3,4,5,6)
group by global_id
order by group_count desc;

我会得到结果:

global_id   group_count
3           15 
5           12

但是想

global_id   group_count
3           15 
5           12
4           0
6           0

这可能吗?我尝试了一些有条款,但似乎无法使其发挥作用。

I have a sql statement like this (using mysql 5.5.15):

select global_id, count(id) as group_count
from globals_lists
group by global_id
order by group_count desc;

and would like to modify it to support a subset with an in statement like this:

select global_id, count(id) as group_count
from globals_lists
where global_id in (3,4,5,6)
group by global_id
order by group_count desc;

I'll get as a result:

global_id   group_count
3           15 
5           12

but would like

global_id   group_count
3           15 
5           12
4           0
6           0

Is this possible? I tried a couple of having clauses but couldn't seem to get it working.

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

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

发布评论

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

评论(3

桃扇骨 2024-12-28 10:22:37

看起来有点尴尬,但是怎么样?

select IDs.ID, ifnull(count(globals_lists.global_id),0) as group_count
from (select 3 as ID
             UNION select 4
             UNION select 5
             UNION select 6) as IDs
left join globals_lists on IDs.ID = globals_lists.global_id
group by IDs.ID
order by group_count desc;

It's a little awkward looking, but how about?

select IDs.ID, ifnull(count(globals_lists.global_id),0) as group_count
from (select 3 as ID
             UNION select 4
             UNION select 5
             UNION select 6) as IDs
left join globals_lists on IDs.ID = globals_lists.global_id
group by IDs.ID
order by group_count desc;
ゞ花落谁相伴 2024-12-28 10:22:37

您可以添加包含所有所需值的表。要么是内联的,如下所示,要么是在临时表中。然后您可以使用left join来匹配行数。

select  lst.nr
,       count(gl.id) as group_count
from    (
        select 3 as nr
        union all select 4
        union all select 5
        union all select 6
        ) lst
left join 
        globals_lists gl
on      lst.nr = gl.global_id
group by 
        lst.nr
order by 
        count(gl.id) desc

You could add a table with all desired values. Either inline, like below, or in a temporary table. Then you can use left join to match the number of rows.

select  lst.nr
,       count(gl.id) as group_count
from    (
        select 3 as nr
        union all select 4
        union all select 5
        union all select 6
        ) lst
left join 
        globals_lists gl
on      lst.nr = gl.global_id
group by 
        lst.nr
order by 
        count(gl.id) desc
情丝乱 2024-12-28 10:22:37

使用 COUNT(*) 而不是 COUNT(id),这样即使没有,它也会返回一个计数(假设 4 & )。确实存在 6 个:

select global_id, count(*) as group_count
from globals_lists
where global_id in (3,4,5,6)
group by global_id
order by group_count desc;

Instead of COUNT(id), use COUNT(*) so it will return a count even if there are none, assuming 4 & 6 do exist:

select global_id, count(*) as group_count
from globals_lists
where global_id in (3,4,5,6)
group by global_id
order by group_count desc;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文