Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql使用相同的列值连接两行,计算某些列值,并返回一行中的所有行_Mysql - Fatal编程技术网

mysql使用相同的列值连接两行,计算某些列值,并返回一行中的所有行

mysql使用相同的列值连接两行,计算某些列值,并返回一行中的所有行,mysql,Mysql,我有一个表名agents\u commissionid、agent\u id、deal\u id、commission、deal\u date列 我想从代理佣金中选择代理id,佣金,其中月(日期交易)=“$month”和年(日期交易)=“$year” 但是,如果表中有一些行具有相同的agent\u id将相同的行连接到一行中,并计算佣金值,如第1行、第2行中的第66666行7+100000 作为最后一个获取 数组('agent_id'=>1,'commission'=>166666.7) 谢谢

我有一个表名
agents\u commission
id、agent\u id、deal\u id、commission、deal\u date列

我想从
代理佣金
中选择
代理id
佣金
,其中月(
日期交易
)=“$month”和年(
日期交易
)=“$year”

但是,如果表中有一些行具有相同的
agent\u id
将相同的行连接到一行中,并计算
佣金
值,如第1行、第2行中的第66666行7+100000

作为最后一个获取

数组('agent_id'=>1,'commission'=>166666.7)


谢谢

尝试使用分组方式和聚合:

select agentid, sum(commission) as commission
from agents_commission
where month(date_deal) = '$month' and year(date_deal) ='$year';
group by agentid

尝试使用分组依据和聚合:

select agentid, sum(commission) as commission
from agents_commission
where month(date_deal) = '$month' and year(date_deal) ='$year';
group by agentid

如果只想按一列(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语句只能用于包含列名、聚合函数、常量和表达式。


请参阅

如果只想按一列(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语句只能用于包含列名、聚合函数、常量和表达式。


参考

非常感谢mauch^非常感谢mauch^^