How to concatenate multiple rows which aren't the primary key

Viewed 63

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 |
+-----------+--------------------------+---------------------+

You get the manufacture_product_str by looking at the item_id's contained in an outfit (aka items_in_outfit table. Then, from that, you find each item_id's manufacturer_product_id and concatenate it accordingly in ascending order of manufacturer_product_id.

How can I do this for a customer's driver_id? My attempt at it didn't make sense because all I can do is join the tables together, but no idea how to get the manufacturer_product_id's in string form like the above output

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
2 Answers

If I understand correctly, you want an aggregation and GROUP_CONCAT() for the manufacturer ids:

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;

Note that I introduced table aliases so the query is easier to write and to read.

If you need more than one row (eg, for more than one outfit_id), you would need a GROUP BY.

Related