简体   繁体   English

mysql将具有相同列值的两行连接起来,并计算某些列值,并在一行中返回所有行

[英]mysql join two rows with same column value and calculate certain column values and return all rows in one row

I have a table name agents_commission have columns id, agent_id, deal_id, commission, deal_date. 我有一个表名称agents_commission包含列id,agent_id,deal_id,佣金,deal_date。

agents_commission

I want to select agent_id , commission from agents_commission where month( date_deal ) = '$month' and year( date_deal ) ='$year'; 我想选择agent_id ,从agents_commission中选择commission ,其中month( date_deal )='$ month'和year( date_deal )='$ year';

but if the table have some rows have the same agent_id join the same rows in one row and calculate the commission values like row 66666.7 + 100000 as in row 1, 2 但是如果表中的某些行具有相同的agent_id则将同一行连接到同一行中,并像第1、2行一样计算commission值,例如第66666.7 + 100000行

to be the final fetch_assoc as 成为最终的fetch_assoc作为

array('agent_id'=>1, 'commision'=>166666.7); array('agent_id'=> 1,'commision'=> 166666.7);

thanks 谢谢

Try using group by and aggregation: 尝试使用分组依据和聚合:

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

You can not select all when you want to group by only one column(agentid). 如果只想按一列(agentid)分组,则不能全部选择。 you can do something like below 你可以做下面的事情

select agent_id, sum(commission) as commission from temp where MONTH(deal_date) = '09' and year(deal_date) ='2018' group by agent_id;

The SELECT statement used in the GROUP BY clause can only be used contain column names, aggregate functions, constants and expressions. GROUP BY子句中使用的SELECT语句只能用于包含列名,聚合函数,常量和表达式。

Refer https://dev.mysql.com/doc/refman/5.5/en/group-by-modifiers.html 参考https://dev.mysql.com/doc/refman/5.5/en/group-by-modifiers.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL查询:当另一列中的值符合特定条件时,返回一列中具有特定值的所有行 - MySQL Query: Return all rows with a certain value in one column when value in another column matches specific criteria MySQL:仅返回一个表中的行,其中另一个表的一列中的所有值都相同 - MySQL: Return only rows in one table where ALL values in one column of another table are the same 如何将具有相同列值的mysql行分组为一行? - How to group mysql rows with same column value into one row? 将具有相同id但不同列值的两行合并为一行[mysql] - merge two rows with same id but different column values into one row [mysql] 过滤所有行的特定条件,并为具有相同值的每一列仅返回一行 - Filtering down specific criteria for all rows and return only one row for each column with the same value MySQL在一行中返回具有多个值的行? - MySQL return rows with multiple values in one column? MySQL联接-检查具有某些列值的不存在的行 - Mysql join - check for non existant rows having certain column values 根据不同列的行之一上的值返回具有相同 id 的所有行 - Return all rows that have the same id based on a value on one of the rows of a different column MySQL从左连接选择行,在一列中具有特定数量的不同值,但没有总行数限制 - MySQL select rows from left join with specific number of distinct values in one column but without total row limit mySQL 连接表,但根据来自另一列的匹配条件排除具有特定 ID/值的所有行(条件仅包含在某些行中) - mySQL Join Tables but exclude ALL rows with a certain ID/value based on matching criteria from another column (criteria contained in only some rows)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM