gpt4 book ai didi

php - Sql选择总和限制忽略

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

我面临着一种情况,我需要避免查询本身中出现重复的数字。

我有这样的代码:

    $rangelimit     = range(6, 60, 6);
$name = "some name";
foreach( $rangelimit as $limit){

$result = $db->query("SELECT SUM(numbers)
FROM (SELECT numbers FROM table WHERE name ='".$name."'
ORDER BY id ASC LIMIT 0,".$limit.")
AS SUBT");

if($result)
echo $limit.' value is .' $result;

}// end foreach

// outputs
// 6 value is 22
// 12 value is 32
// 18 value is 40
// but here is the problem once there is no more results
// i want it to stop and give 0
// 24 value is 40
// 30 value is 40 ... and keeps going on till 60 as set on the range.

我的问题与查询相关,因为我可以在 foreach 循环中停止它,但我想知道是否有任何方法可以告诉查询在没有达到限制的结果时停止?

最佳答案

如果我理解正确的话,当限制中没有足够的行时,您希望总和为 0。方法如下:

SELECT (case when count(*) = ".$limit."
then SUM(numbers)
else 0
end)
FROM (SELECT numbers
FROM table
WHERE name ='".$name."'
ORDER BY id ASC LIMIT 0,".$limit."
) SUBT;

关于php - Sql选择总和限制忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21897633/

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