我正在尝试显示用 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 关注
- 96 浏览
添加回答
举报
0/150
提交
取消