我有下列表格
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
有两套装备
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
. 如何选择所有选项 cus_id=2's
连在一起的一串 manufacturer_product_id'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 |
+-----------+--------------------------+---------------------+
但是我得到的结果是
+-----------+--------------------------+---------------------+
| outfit_id | manufacturer_product_ids | creation_date |
+-----------+--------------------------+---------------------+
| 1 | 2,2,5,383,383,2131,34634 | 2020-01-28 12:31:00 |
+-----------+--------------------------+---------------------+
基本上是把所有的 outfit_id = 1
以及 outfit_id = 2
一起。我希望每件衣服都是分开的。
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;
1条答案
按热度按时间dtcbnfnu1#
你需要加上
group by
条款