gpt4 book ai didi

MySQL - 按范围分组

转载 作者:可可西里 更新时间:2023-11-01 06:31:37 25 4
gpt4 key购买 nike

我需要计算值范围内的记录。

例如:对于集合 1, 7, 9, 23, 33, 35, 1017

select count(myvalue) group by round(myvalue/10) 给出如下内容:

0-10  -> 3
10-20 -> 0
20-30 -> 1
30-40 -> 2
1010-1020 -> 1

这很好用。但是,我需要设置一个上限,以便 MySQL 返回 40+ --> 1 ?如何实现?

最佳答案

您可以在客户端对值求和或使用两个查询(可能带有union)来获取数据,例如:

select round(myvalue / 10), count(myvalue) from table where myvalue < 40 group by round(myvalue / 10)
union
select '40+', count(myvalue) from table where myvalue >= 40

绝对可以将它写在带有子查询或复杂条件的单个查询中,但它不会那么简单和可维护。

关于MySQL - 按范围分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17061156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com