1

我有以下 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 |
                        +-------------------+--------+

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

  1. 'serial'中的cid是表'company'中的公司 ID
  2. “产品”中的nr定义了所需产品的序列号的前三位数字。

我需要做的是这样的输出:

+-------------+--------------------------------------+
| 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 查询来解决这个问题,以获得如上所述的输出?

4

1 回答 1

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
于 2013-07-11T16:10:22.647 回答