How can I make a LEFT JOIN, but with rows separed from its relations in MySQL?
I need to get all rows that are in the table A, but joining with the table B (basically a LEFT JOIN
), but also, I need to get the A table row itself, for example, with these tables:
Table A:
id | name |
---|---|
1 | Random name |
2 | Random name #2 |
Table B:
id | parent_id | location |
---|---|---|
1 | 2 | Location #1 |
2 | 2 | Location #2 |
With this query:
SELECT * FROM A
LEFT JOIN B
ON A.id = B.parent_id;
I get something like this:
id | name | id | parent_id | location |
---|---|---|---|---|
1 | Random name | NULL | NULL | NULL |
2 | Random name #2 | 1 | 2 | Location #1 |
2 | Random name #2 | 2 | 2 | Location #2 |
But I want to get something like this:
id | name | id | parent_id | location |
---|---|---|---|---|
1 | Random name | NULL | NULL | NULL |
2 | Random name #2 | NULL | NULL | NULL |
2 | Random name #2 | 1 | 2 | Location #1 |
2 | Random name #2 | 2 | 2 | Location #2 |
As you can see, there is a row by itself of "Random name #2" separated from its joins, how can I do that?
The main idea is that there are an ads
table (the table A), but also, there are a subads
table (the table B) with little variations of the ads
table, and I need to show all ads and subads in a unique query.
Tanks a lot!
from Recent Questions - Stack Overflow https://ift.tt/2UdCB48
https://ift.tt/eA8V8J
Comments
Post a Comment