-1

我想编写一个 php 脚本来比较数据库中的行,然后如果 payment_id 匹配,则将这些值相加。基于payment_id:

例子:

   +----+------------+-----------+--------+
   | id | payment_id | cheque_id | amount |
   +----+------------+-----------+--------+
   |  1 | 1000       | MB101     | 20     |
   |  2 | 1000       | MB102     | 20     |
   |  3 | 1111       | MB113     | 20     |
   +----+------------+-----------+--------+

需要结果

   +------+--------------+----+
   | 1000 |  MB101/MB102 | 40 |
   | 1111 |  MB113       | 20 |
   +------+--------------+----+

尝试将检查列合并为字符串。对于“金额”列,我知道应该使用 SUM。

任何建议表示赞赏,

谢谢

4

2 回答 2

2

采用GROUP BY

select payment_id,group_concat(cheque_id SEPARATOR '/') as cheque_ids,
SUM(amount) as amount
FROM table name
GROUP BY payment_id
于 2012-04-06T07:14:32.747 回答
0

我找到了解决方案

SELECT payment_id, group_concat(cheque_id SEPARATOR '/') AS cheque_ids,
SUM(amount) AS amount
FROM tbl_name
GROUP BY payment_id
于 2012-07-16T09:26:02.453 回答