MySQL 不带 Order By 的最大计数
我有以下 MySQL 行:
SELECT Age, count(*) AS Total FROM pacient WHERE Age BETWEEN 20 AND 40 GROUP BY Age ORDER BY Age
我需要向其中添加一个仅显示的附加列每行的 count(*) 的最大值。例如:
13 2 7
18 2 7
23 5 7
24 7 7
26 6 7
32 3 7
38 1 7
41 1 7
46 4 7
这将是 3 列,第三列显示 7,因为 7 是进行 count(*) 的第二列中的最大数字。
I have the following MySQL line:
SELECT age, count(*) AS total FROM pacient WHERE age BETWEEN 20 AND 40 GROUP BY age ORDER BY age
and I need to add an additional column to it that shows ONLY the max value of the count(*) for every row. For example:
13 2 7
18 2 7
23 5 7
24 7 7
26 6 7
32 3 7
38 1 7
41 1 7
46 4 7
This would be 3 columns and the 3rd column shows 7 since 7 was the highest number in the second column where the count(*) is made.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里是解决方案:
Here the solution:
您是否尝试过用另一个查询包装您的查询?类似的东西
Have you tried to wrap your query with another query? something like
通过在没有连接条件的情况下对第二个“子选择”进行连接,它将给出笛卡尔结果...由于它是没有分组依据的计数,因此它将始终返回单个记录,从而连接到所有年龄行,否则。该值的 MAX() 没有问题,因为它是唯一的记录将按原样返回。
By doing a join to the second "subselect" with no join condition, it will give a Cartesian result... Since it is a count with no group by, it will always return a single record and thus be joined to all age rows otherwise. The MAX() of the value is no problem since it is the only record will just be returned as-is.
使用 SQL 视图而不是使用子查询总是好的。因为 VIEW 将具有已经编译的结果。
It is always good to use SQL VIEWS instead of using sub queries. Because VIEW will be having already compiled result.