I have the following table :
+--------------------+-----------+--------------------+
| Product | website | is_master |
+--------------------+-----------+--------------------+
| product A | X | 1 |
| product A | Y | 0 |
| product A | Z | 0 |
| product A | C | 0 |
| product B | D | 1 |
| product C | E | 1 |
+--------------------+-----------+--------------------+
I am trying to make a mysql query to get a table a as the following one :
+--------------------+------------------+--------------------+
| Product | master_website | additional_sites |
+--------------------+------------------+--------------------+
| product A | X | y,z,c |
| product B | D | null |
| product C | E | null |
+--------------------+------------------+--------------------+
I have tried with a subselect query but I failed in both cases.
select
Product,
(select Product
FROM `table1`
LEFT JOIN
table2 on table1.id = table2.fk_table1
WHERE is_master = 1) is_master,
(select group_concat(Product)
FROM `table1`
LEFT JOIN
table2 on table1.id = table2.fk_table1
WHERE is_master = 0) additional
FROM `table1`
LEFT JOIN
table2 on table1.id = table2.fk_table1
WHERE 1
group by
Product
The problem is that the subselect returns more than one row.
SELECT t1.Product,
t1.website AS master_website,
GROUP_CONCAT(t2.website ORDER BY t2.website) AS additional_sites
FROM MyTable t1
LEFT JOIN MyTable t2
ON t1.Prduct = t2.Prduct AND
t2.is_master = 0
WHERE t1.is_master = 1
GROUP BY t1.Product