If I need to fetch rows from table A with a matching tag in table B, I’ll just do a simple JOIN ON/WHERE B.name=X. But MySQL will then only return the row from A and the matching tag from B.
What do I do if I want to retrieve all from table A that has a match in table B, and at the same time retrieve all from table B that are linked to the row in table A.
A:
a.id
1
2
3
B:
b.id | b.a_id | b.name
1 | 1 | foo
2 | 1 | bar
3 | 1 | derp
4 | 2 | foo
5 | 2 | derp
6 | 3 | bar
A search for "foo" should then return
a.id | b.id | b.a_id | b.name
1 | 1 | 1 | foo
1 | 2 | 1 | bar
1 | 3 | 1 | derp
2 | 4 | 2 | foo
2 | 5 | 2 | derp
Appreciate any help I can get. Thanks!
I think this is what you are looking for. Might not be the most performant solution, but I hope it will work.
Edit:
I don’t think it’ll get any better than this, basically you don’t need to query the table a anymore; you fetch all rows from b that match ‘foo’, then join those with all relating items.