我有一个表名 agents_commission
有列 id、agent_id、deal_id、commission、deal_date。
我想从 agents_commission
中选择 agent_id
, commission
where month(date_deal
) = '$month'和年份(date_deal
)='$year';
但是如果表中有一些行具有相同的 agent_id
将相同的行连接到一行中并计算 commission
值,例如第 1 行中的第 66666.7 + 100000 行, 2
成为最终的fetch_assoc as
array('agent_id'=>1, 'commision'=>166666.7);
谢谢
最佳答案
当您只想按一列(agentid)分组时,您不能选择全部。你可以做如下的事情
select agent_id, sum(commission) as commission from temp where MONTH(deal_date) = '09' and year(deal_date) ='2018' group by agent_id;
GROUP BY 子句中使用的 SELECT 语句只能用于包含列名、聚合函数、常量和表达式。
引用https://dev.mysql.com/doc/refman/5.5/en/group-by-modifiers.html
关于mysql连接具有相同列值的两行并计算某些列值并返回一行中的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52200783/