如何在mysql中找到相关的字段月份

时间:2017-11-14 07:47:57

标签: mysql sql

这是我的表架构

月BD名称付款金额总付款金额

July,2017   Mohit Siwal       70                 210      -140
August,2017 Mohit Siwal       350                150      200
September 2017  Mohit Siwal    277               556        -279
September 2017  Mohit Siwal     500                 250    250
September 2017  Mohit Siwal   250               500         -250
November 2017   Mohit Siwal     200              100    100

我想添加所有九月份,并在此表中添加相关字段;

来自输出

Month        BD Name    Payment Amount        Total Paid    Due Amount
July 2017   Mohit Siwal      70                      210    -140
August 2017 Mohit Siwal      350                     150    200
September 2017  Mohit Siwal   1027                 1306     -279
November 2017   Mohit Siwal    200                    100   100

我写了这个查询

$countryquery="select id, project_name, SUM(total_paid) as t, bd_id, payment_date from `tbl_payments`where bd_id=$bid Group By project_name order by payment_date" ;

1 个答案:

答案 0 :(得分:0)

您可以按月和bd_name分组并查找金额总和:

select month,
    bd_name,
    sum(payment_amount) as payment_amount,
    sum(total_paid) as total_paid,
    sum(due_amount) as due_amount
from your_table
group by month,
    bd_name
order by month;