I have following tables:
- table1 id, user_id, first_name, last_name
- table2 id, user_id, company_name, first_name, last_name, partner_id
- table3 businesses id, business_name
I want to create query which will return rows of data: user_id, (first_name last_name), (business_name, business_name ...) When user is associated with multiple businesses, something like this: 123, John Doe, Business1, Business2
I can get kind of duplicate rows when I use following query:
SELECT table1.first_name, table1.last_name, table2.copmany_name,
(case when concat(table1.first_name, table1.last_name) = '' then table2.company_name else concat_ws(' ', table1.first_name,table1.last_name) end) as name,
table3.name as business_name
FROM table1
JOIN table2 ON table1.user_id = table2.user_id
JOIN table3 ON table2.partner_id = table3.id
Here is sample from that query:
123, John Doe, Business1
123, John Doe, Business2
125, Marie Bird, Business3
And I want to get:
123, John Doe, Business1, Business2
125, Marie Bird, Business3
Is that possible?
Thank you
||
– Hambone