I have the following tables
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 |
+----------------+-----------+---------+
Just to summarize the above. The customer
sam
has 2 outfits with the outfit_id's
1
and 2
. Outfit 1
contains the items_id's
, 2,3,4
. Outfit 2
contains the items_id's
, 1,2,3,5
.
item_id's
are useless bits of data but I need it to hold primary key integrity. What I really care about is the manufacturer_product_id
. How do I select all of cus_id=2's
outfits in a concatenated string of manufacturer_product_id's
. So my output would be:
+-----------+--------------------------+---------------------+
| 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 |
+-----------+--------------------------+---------------------+
However the output I get is
+-----------+--------------------------+---------------------+
| outfit_id | manufacturer_product_ids | creation_date |
+-----------+--------------------------+---------------------+
| 1 | 2,2,5,383,383,2131,34634 | 2020-01-28 12:31:00 |
+-----------+--------------------------+---------------------+
Which is essentially combining all of outfit_id = 1
and outfit_id = 2
together. I want each outfit_id to be separate.
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;
You need to add group by
clause
SELECT o.outfit_id, o.creation_date,
GROUP_CONCAT(i.manufacturer_product_id ORDER BY i.manufacturer_product_id) as manufacturer_product_ids,
GROUP_CONCAT(i.manufacturer_product_id ORDER BY i.manufacturer_product_id SEPARATOR ';') as manufacturer_product_ids_sep
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
group by o.outfit_id,o.creation_date
ORDER BY o.creation_date
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.