mysql - MySQL 中的这个 group by 查询有什么问题?

标签 mysql sql join group-by

我的数据库结构:
enter image description here
每个“wp_custombill”都有多个“wp_customuser”

我想查询所有的custombill,每个custombill 都有其customuser 的总和。我的查询是

select cb.ID, cb.Description, cb.amount, SUM(cu.amount) 
from wp_customuser as cu 
inner join wp_custombill as cb 
    on cu.email = cb.Email
group by cb.ID, cb.Description, cb.amount

但结果似乎是 sum(cu.Amount) 汇总了所有 CustomUser 的记录。 enter image description here

最佳答案

我猜你的 wp_custombill ids (1,2,3) 有相同的 email 但是没有数据样本是很难测试的。

否则你的查询看起来没问题。

关于mysql - MySQL 中的这个 group by 查询有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32470572/

相关文章:

mysql - 从字段 sql/presto 中提取子字符串

mysql - 使用 MySQL 创建简单修订系统的最佳方法是什么?

mysql - 有点复杂的 MySQL 语句

php - 将下拉菜单中的值分配给 Controller 中的变量,该变量在 Codeigniter 的模型中使用

java - JPA 加入复合键的一部分

php - 使用联接来组合两个查询

php - PDO 即使没有绑定(bind)任何参数仍然会得到结果

mysql - 如何删除基于非主键列的重复行

php - 使用连接的mysql不同查询

php - PDO 参数未传递,但 sprintf 已传递