gpt4 book ai didi

php - SQL匹配表

转载 作者:行者123 更新时间:2023-11-29 05:19:55 26 4
gpt4 key购买 nike

我有两个查询结果,一个是:

amount | percent
100 | 10
200 | 20
300 | 30
400 | 40

第二个是:

sales
10
20
110
120
210
220
310
320

有没有办法得到这样的东西:

sales                      | percent
30 (this is sum below 100) | 10
230(this is sum between 100 and 200) | 20
430(this is sum between 200 and 300) | 30
630(this is sum between 300 and 400) | 40

我想用 PHP 数组和循环来完成它,但我不想向服务器发送很多请求。有什么建议吗?

最佳答案

子查询会多次执行查询,不好用php做会更好像这样(而不是查询我有 $first 和 $second 数组)

$first = array(
array(
'amount' => 100,
'percent' => 10
),
array(
'amount' => 200,
'percent' => 20
),
array(
'amount' => 300,
'percent' => 30
),
array(
'amount' => 400,
'percent' => 40
)
);
$second = array(
array(
'sales' => 10
),
array(
'sales' => 20
),
array(
'sales' => 110
),
array(
'sales' => 120
),
array(
'sales' => 210
),
array(
'sales' => 220
),
array(
'sales' => 310
),
array(
'sales' => 320
)
);

$result = array();
$second_len = count($second);
$s=0;
for ($i=0,$len=count($first); $i < $len; $i++) {
$sum = 0;
for (;$s < $second_len; $s++) {
if ($second[$s]['sales'] <= $first[$i]['amount']) {
$sum += $second[$s]['sales'];
} else {
break;
}
}
$result[] = array(
'sales' => $sum,
'percent' => $first[$i]['percent']
);
}

var_dump($result);

关于php - SQL匹配表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27343211/

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