我正在嘗試顯示用 date 和 username 分隔的用戶付款i.e (GROUP BY date, username)。這是數據庫表id | username | income | date | | |1 | super | 750 | 2019-09-212 | super | 750 | 2019-09-213 | super | 1 | 2019-09-224 | super | 750 | 2019-09-22這是我正在嘗試的代碼$income = 0;while($row = mysqli_fetch_array($query)){ $income += $row['income'];}當我明智地看到記錄日期時,這就是我得到的輸出id | username | income | date | | |1 | super | 750 | 2019-09-212 | super | 751 | 2019-09-22這就是我所期待的id | username | income | date | | |1 | super | 1500 | 2019-09-212 | super | 751 | 2019-09-22我正在嘗試的第二個選項是sql查詢SELECT id, username, income, COUNT(*) AS total, date FROM paymentGROUP BY date, usernamephp代碼$income = 0;while($row = mysqli_fetch_array($query)){ $income = $row['income'] * $row['total'];}這就是我得到的id | username | income | date | | |1 | super | 1500 | 2019-09-212 | super | 2 | 2019-09-22我期待著id | username | income | date | | |1 | super | 1500 | 2019-09-212 | super | 751 | 2019-09-22在我看來,這個問題是由于value 1在行中發生的,但不知道如何解決。任何建議將不勝感激注意 -列3rd, 5th, 7th and 10th row的income將包含值1
1 回答

GCT1015
TA貢獻1827條經驗 獲得超4個贊
如果您按用戶名和日期對其進行分組,則需要將所有行的收入相加(使用SUM())
所以SQL應該是......
SELECT date, username, SUM(income) AS total
FROM payment
GROUP BY date, username
- 1 回答
- 0 關注
- 113 瀏覽
添加回答
舉報
0/150
提交
取消