I have a MySQL table of stores data. These stores have one or more phone numbers stored in another table and connected by the foreign store_id .
This query doubles my stores for every phone number it finds. Therefore, it returns a very repeated number of stores:
SELECT a.*,b.numero_telefono
FROM tiendas AS a
INNER JOIN telefonos AS b ON a.id = b.id_tienda
| id |nombre_tienda| telefono |
| -- | ------- | ----------- |
| 1 | Tienda A | 5555555 |
| 1 | Tienda A | 6666666 |
| 1 | Tienda A | 7777777 |
| 2 | Tienda B | 1234567 |
| 3 | Tienda C | 8888888 |
| 3 | Tienda C | 9999999 |
| 4 | Tienda D | 1111111 |
What I need is to adjust the query so that it collects all the numbers stored in a single cell, respecting the number of stores. Something like this:
| id |nombre_tienda| telefono |
| -- | ------- | ------------ |
| 1 | Tienda A | 5555555,6666666,7777777 |
| 2 | Tienda B | 1234567 |
| 3 | Tienda C | 8888888,9999999 |
| 4 | Tienda D | 1111111 |
You occupy the function
GROUP_CONCAT()
in this way:You must group by the columns of table a, as much
id
asnombre_tienda
GROUP_CONCAT