gpt4 book ai didi

mysql - SQL 按总和过滤

转载 作者:行者123 更新时间:2023-11-29 13:38:59 25 4
gpt4 key购买 nike

如何按总和过滤一组元素?假设我有:

id
1
2
3
4

是否有一个子句我可以使用,例如:

select * from numbers where sum = 6

请注意,该查询不会这样做,它是一个或多或少解释我的意思的示例。

这个子句会返回 1、2 和 3?

最佳答案

我猜测您希望累积和等于 6。这是使用标准 SQL 的一种方法:

select id
from numbers n
where 6 = (select sum(n2.id) from numbers n2 where n2.id <= n.id);

编辑:

哎呀,搞反了:

select id
from numbers n
where 6 >= (select sum(n2.id) from numbers n2 where n2.id <= n.id);

但是,即使没有值加起来正好是 6,这也会返回值。

关于mysql - SQL 按总和过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18370622/

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