sql - 在子查询中别名 count * 的正确方法
问题描述
我在下面有查询
select t.comment_count, count(*) as frequency
from
(select u.id, count(c.user_id) as comment_count
from users u
left join comments c
on u.id = c.user_id
and c.created_at between '2020-01-01' and '2020-01-31'
group by 1) t
group by 1
order by 1
当我也尝试给错误起别名count(*)
时count(t.*)
,我不能用表中的 t 来别名吗?不知道我错过了什么
谢谢
解决方案
Count(*)
代表查询返回的所有行的计数(相对于GROUP BY
列)。因此,指定所涉及的表之一是没有意义的。例如,考虑计算连接产生的行数。如果您需要特定表的行数,t
您可以使用count(distinct t.<unique column>)
推荐阅读
- swift - 如果使用 return,Swift 中的 Guard 语句会出错
- swift - 如何检测选择了哪个 NSMenuItem
- .net - 当电子邮件地址没有 @ 符号时,电子邮件 RegEx 陷入无限循环
- vb.net - 在 VB.NET 中获取进程的名称
- javascript - 电子应用程序未启动
- swift - 如何获取位于 collectionviewcell 内的 TextField 文本?
- google-cloud-platform - 我无法在谷歌云平台上删除我的项目
- android - 无法获取节点的值
- android-studio - 您的应用程序当前的目标是 API 级别 23。但是,它必须至少将 API 级别 26 设为目标才能从最新的 API 中受益
- javascript - WebGL 错误:GL_INVALID_FRAMEBUFFER_OPERATION