I hope someone can help, I’ve been trawling SO for hours and can’t find an answer that does what I need (or at least I can’t see how the answers apply to my situation)
So I have 3 tables, Horses, owners, and horse_owners
Table: horses
ID Name age
1 arkle 3
2 shergar 4
3 daisy 2
Table: owners
ID Name
1 Joe
2 Jack
3 Susan
4 Mike
Table: horse_owners
owner_id horse_id
1 1
2 1
3 1
4 2
There are other fields but for the sake of simplicity I’ve removed them.
I want to run a query that lists the horses, and their owners (if they have any).
Here’s what I have now:
SELECT
h.id,
h.name,
h.age
GROUP_CONCAT(o.name separator ', ') as owners
FROM
horses h
LEFT JOIN
horse_owners ho
ON
h.id = ho.horse_id
LEFT JOIN
owners o
ON
ho.owner_id = o.id
I’m using the group_concat function to combine the owners names where some have multiple owners. But the problem is the query only returns horses that have owners. I want to see all horses whether they have owners or not.
1 Answer