gpt4 book ai didi

php - 如何使用 SUM(CASE) 和日期间隔来选择用户及其金额列表和总金额

转载 作者:行者123 更新时间:2023-11-29 15:34:56 25 4
gpt4 key购买 nike

我有一个表,其中包含用户列表以及他们每天节省的金额。我想选择他们每天节省的所有钱,并列出他们节省的所有详细信息(金额)。

问题:我找不到该日期间隔的总和

enter image description here

SELECT 
cust_name,
ac_no,
amount,
SUM(CASE WHEN (trans_date > DATE_SUB(NOW(), INTERVAL 1 MONTH))
THEN amount ELSE 0 END) as todalTotal from contributions
where username = 'james12'
GROUP BY id

这就是我想要实现的目标

$que = mysqli_query($con,"SELECT  cust_name,
ac_no,
amount FROM contributions
WHERE username='$username'
"
);
while($row = mysqli_fetch_array($que))
{

$sql =mysqli_query($con,"SELECT SUM(amount) as total FROM contributions WHERE trans_date > DATE_SUB(NOW(), INTERVAL 3 DAY) AND username='$username' ");

$rows = mysqli_fetch_array($sql);

echo $rows["total"] . "<br>";
echo $row["cust_name"] . "<br>";
}

$rows["total"] will appear the number of times $row["cust_name"] appears which I don't want. I want $rows["total"] to appear just once.

最佳答案

您必须选择列,然后告诉它所在的位置。然后只需使用 SUM 即可。

SELECT SUM(amount) AS AmountSaved
FROM yourtable

SQLdatabase

关于php - 如何使用 SUM(CASE) 和日期间隔来选择用户及其金额列表和总金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58341660/

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