【问题标题】:How to concatenate multiple rows which aren't the primary key如何连接不是主键的多行
【发布时间】:2020-11-09 05:00:42
【问题描述】:

我有以下表格

Customer (driver_id is UNQIUE)
+--------+------+-----------+
| cus_id | name | driver_id |
+--------+------+-----------+
|      1 | bob  |      2342 |
|      2 | sam  |      2463 |
+--------+------+-----------+

Items (manufacture_product_id is UNIQUE) 
+---------+-------+-------------------------+
| item_id | name  | manufacturer_product_id |
+---------+-------+-------------------------+
|       1 | shirt |                    2131 |
|       2 | jeans |                     383 |
|       3 | pants |                       2 |
|       4 | watch |                   34634 |
|       5 | belt  |                       5 |
+---------+-------+-------------------------+

Outfits
+-----------+--------+---------------------+
| outfit_id | cus_id |    creation_date    |
+-----------+--------+---------------------+
|         1 |      2 | 2020-01-28 12:31:00 |
|         2 |      2 | 2020-01-29 15:23:12 |
+-----------+--------+---------------------+

items_in_outfit
+----------------+-----------+---------+
| outfit_item_id | outfit_id | item_id |
+----------------+-----------+---------+
|              1 |         1 |       2 |
|              2 |         1 |       3 |
|              3 |         1 |       5 |
|              4 |         2 |       1 |
|              5 |         2 |       2 |
|              5 |         2 |       3 |
|              6 |         2 |       5 |
+----------------+-----------+---------+

只是总结以上。 customer sam 有 2 套服装,分别是 outfit_id's 12。服装1 包含items_id's2,3,4。服装2 包含items_id's1,2,3,5

item_id's 是无用的数据位,但我需要它来保持主键完整性。我真正关心的是manufacturer_product_id。如何选择串联的manufacturer_product_id's 字符串中的所有cus_id=2's 服装。所以我的输出是:

+-----------+--------------------------+---------------------+
| outfit_id | manufacturer_product_str |    creation_date    |
+-----------+--------------------------+---------------------+
|         1 | 2,383,34634              | 2020-01-28 12:31:00 |
|         2 | 2,5,383,2131             | 2020-01-29 15:23:12 |
+-----------+--------------------------+---------------------+

您可以通过查看服装中包含的item_id's(又名items_in_outfit 表)来获得manufacture_product_str。然后,从中找到每个item_id's manufacturer_product_id,并以@ 的升序相应地连接它987654343@.

如何为客户的 driver_id 执行此操作?我的尝试没有意义,因为我所能做的就是将表格连接在一起,但不知道如何以字符串形式获取manufacturer_product_id's,如上面的输出

SELECT outfit_id, creation_date
FROM outfits
INNER JOIN items_in_outfit ON outfits.outfit_id = items_in_outfit.outfit_id
INNER JOIN customer ON outfits.cus_id = customer.cus_id
WHERE items customer.driver_id = 2463
ORDER BY outfits.creation_date

【问题讨论】:

    标签: mysql sql database select concatenation


    【解决方案1】:

    如果我理解正确,您需要一个聚合和GROUP_CONCAT() 作为制造商 ID:

    SELECT o.outfit_id, o.creation_date,
           GROUP_CONCAT(i.manufacturer_product_id ORDER BY i.manufacturer_product_id
                       ) as manufacturer_product_ids
    FROM outfits o INNER JOIN
         items_in_outfit io
         ON o.outfit_id = io.outfit_id JOIN
         items i
         ON io.item_id = i.item_id JOIN
         customer c
         ON o.cus_id = c.cus_id 
    WHERE c.driver_id = 2463
    ORDER BY o.creation_date;
    

    请注意,我引入了表别名,以便查询更易于编写和阅读。

    【讨论】:

      【解决方案2】:

      如果您需要多行(例如,对于多个装备 ID),则需要 GROUP BY

      【讨论】:

        猜你喜欢
        • 2019-03-04
        • 2021-04-02
        • 2019-04-24
        • 1970-01-01
        • 2019-12-28
        • 2020-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多