【问题标题】:MySQL Statement to joins 2 tables and sum same items qtyMySQL 语句连接 2 个表并对相同项目数量求和
【发布时间】:2015-08-27 04:38:28
【问题描述】:

我有 2 个表,客户和交易。

Customer:
--------------------------------------------
ID | Name            | Tel
--------------------------------------------
1  | Peter           | 123 4567
2  | John            | 456 1234
3  | Alice           | 789 4561
4  | Amy             | 741 8525

Transaction:
--------------------------------------------
CustID | Books | Pens  | Ruler
--------------------------------------------
  1    |   2   |  0    |   1
  2    |   1   |  0    |   0
  1    |   0   |  3    |   0
  1    |   0   |  0    |   1
  2    |   1   |  1    |   1
  3    |   0   |  2    |   2

我需要以下内容

Results:
-------------------------------------------------------------------
ID | Name          | Tel            | Books | Pens  | Ruler
-------------------------------------------------------------------
1  | Peter         | 123 4567       |   2   |   3   |   2
2  | John          | 456 1234       |   2   |   1   |   1
3  | Alice         | 789 4561       |   0   |   2   |   2
4  | Amy           | 741 8525       |   0   |   0   |   0

基本上它会将同一客户的书籍、钢笔和尺子相加。

我试过了:

                        $sql = "select 
                                    `customer`.id,
                                    `custmaster`.name,
                                    `custmaster`.tel,
                                    `transaction`.id,
                                    `transaction`.books,
                                    `transaction`.pens,
                                    `transaction`.ruler,
                                from `customer` 
                                left join `transaction` 
                                on `customer`.id=`transaction`.custid 
                                ORDER BY `customer`.id ASC";

但不显示。 :( 我确实知道我在某个地方需要 sum() 函数。有人可以帮忙吗?

【问题讨论】:

    标签: php mysql web-application-project


    【解决方案1】:

    使用SUMGROUP BY

    SELECT c.id, c.name, c.tel, SUM(t.books) as books, SUM(t.pens) AS pens, SUM(t.ruler) AS ruler
    FROM customer AS c
    LEFT JOIN transactions AS t ON c.id = t.custid
    GROUP BY c.id
    ORDER BY c.id
    

    【讨论】:

    • 不错...快捷的表名便于参考。
    【解决方案2】:

    试试这个方法

    select 
        `customer`.id,
        `custmaster`.name,
        `custmaster`.tel,
        `transaction`.id,
        sum(`transaction`.books) as books,
        sum(`transaction`.pens) as pens,
        sum(`transaction`.ruler) as ruler,
    from `customer` 
    left join `transaction` 
    on `customer`.id=`transaction`.custid
    Group by `customer`.id,`customer`.Name
    ORDER BY `customer`.id ASC";
    

    【讨论】:

    • 我需要有 2 个分组依据吗?或者我可以按customer.id 分组吗?我对两者都进行了测试,都给出了相同的结果。
    • 没有意义选择transaction.id,因为您正在汇总所有交易。
    • @SykesTang Strict SQL 表示您必须在 GROUP BY 子句中列出所有非聚合列。但是 MySQL 允许您将它们排除在外,因此您只需要定义组的列。
    猜你喜欢
    • 2023-01-30
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多