php—基于另一个表日期的表中数据的总和(成本)

eulz3vhy  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(291)

如何将表1的类别在表2中的每2个日期行之间求和excel表1、2和结果
我试过这样的方法:

SELECT SUM(cost) FROM table1 WHERE date
BETWEEN 
(SELECT date FROM table2 WHERE (line 2,3,4)) 
AND 
(SELECT date FROM table2 WHERE (line 1,2,3)) 
GROUP BY category

但问题是它只给出2&1或3&2或4&3之间的结果,但不能在一个代码中得到所有结果

cygmwpex

cygmwpex1#

<?php
$date_1 = mysqli_query($main_link, "SELECT date FROM table2 ORDER BY id DESC");
$date_2 = mysqli_query($main_link, "SELECT date FROM table2 ORDER BY id DESC LIMIT 12 OFFSET 1");
while($row1 = mysqli_fetch_array($date_1)){
while($row2 = mysqli_fetch_array($date_2)){
    $date_1st = date("Y-m-d", strtotime("{$row1['date']} -1 day"));
    $date_2nd = date("Y-m-d", strtotime("{$row2['date']}"));
    $quary = mysqli_query($main_link, "SELECT category, SUM(cost) FROM table1 WHERE date BETWEEN '$date_2nd' AND '$date_1st'");
    while($row3 = mysqli_fetch_array($quary)){
        echo "{$row3['category']} : {$row3['SUM(cost)']}<br><br>";
    }echo"<hr>";
break;}
}
?>

相关问题