组合三个表并使用mysql计算特定值

时间:2013-07-11 15:59:12

标签: php mysql mysqli

我有以下mySQL表结构:

Table 'company'         Table 'serial'                   Table 'product'
+----+--------------+   +-------------------+--------+   +-----+---------+
| id | name         |   | serial            |cid     |   | nr  | name    |
+----+--------------+   +-------------------+--------+   +-----+---------+
|  1 | One Inc.     |   | 100A000001        |      1 |   | 100 | Phone   |
|  2 | Two Corp.    |   | 100A000002        |      1 |   | 101 | Ball    |
|  3 | Three Corp.  |   | 102A000003        |      1 |   | 102 | Egg     |
|  4 | Four Inc.    |   | 103A000004        |      2 |   | 103 | Box     |
|  5 | Five Inc.    |   | 103A000005        |      3 |   | 104 | Cable   |
+----+--------------+   | 101A000006        |      4 |   +-----+---------+
                        | 102A000007        |      4 |
                        | 103A000011        |      5 |
                        | 103A000010        |      5 |
                        | 104A000007        |      5 |
                        | 104A000008        |      5 |
                        | 103A000009        |      5 |
                        +-------------------+--------+

简短描述它们如何协同工作:

    'serial'中的
  1. cid '公司'的公司ID 'product'中的
  2. nr 定义了序列的前三位数如何与所需产品一致。
  3. 我需要做的是像这样的输出:

    +-------------+--------------------------------------+
    | Customer    | Products                             |
    +-------------+--------------------------------------+
    | One Inc.    | Phone (2), Egg (1)                   |
    +-------------+--------------------------------------+
    | Two Corp.   | Box (1)                              |
    +-------------+--------------------------------------+
    | Three Corp. | Box (1)                              |
    +-------------+--------------------------------------+
    | Four Inc.   | Phone (1), Ball (1)                  |
    +-------------+--------------------------------------+
    | Five Inc.   | Box (3), Cable (2)                   |
    +-------------+--------------------------------------+
    

    但我不知道如何编写查询!

    我目前的解决方案是以下查询:

    SELECT
        c.name,
        s.serial
    FROM
        company c
    INNER JOIN
        serial s
    ON
        c.id = s.cid
    ORDER BY
        c.name DESC
    

    这会生成如下输出:

    +-------------+--------------+ 
    | Customer    | Serial       |
    +-------------+--------------+ 
    | One Inc.    | 100A000001   |
    | One Inc.    | 100A000002   |
    | One Inc.    | 102A000003   |
    | Two Corp.   | 103A000004   |
    | Three Corp. | 103A000005   |
    | Four Inc.   | 101A000006   |
    | Four Inc.   | 102A000007   |
    | Five Inc.   | 103A000011   |
    | Five Inc.   | 103A000010   |
    | Five Inc.   | 104A000007   |
    | Five Inc.   | 104A000008   |
    | Five Inc.   | 103A000009   |
    +-------------+--------------+
    

    目前我正在使用这个输出并且使用PHP我正在生成所需的输出,但是如果没有太多PHP就可以做到这一点。

    所以我要求的是: 如何通过一个很好的mySQL查询来解决这个问题,以获得如上所述的输出?

1 个答案:

答案 0 :(得分:2)

SELECT Customer, GROUP_CONCAT(CONCAT(pname, ' (', pcount, ')')) Products
FROM (
    SELECT
        c.name Customer,
        p.name pname,
        COUNT(*) pcount
    FROM
        company c
    INNER JOIN
        serial s
    ON
        c.id = s.cid
    INNER JOIN
        product p
    ON p.nr = LEFT(s.serial, 3)
    GROUP BY Customer, pname) x
GROUP BY Customer
ORDER BY
    Customer DESC