【发布时间】: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 1 和 2。服装1 包含items_id's、2,3,4。服装2 包含items_id's、1,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