Mysql Joins - Using Left Join and Right Join To Find Orphan Rows
Mysql Joins - Using Left Join and Right Join To Find Orphan Rows
If you're joining several tables together, you can do so in such a way that you show only the combined results, or in
such a way that you can also (or only) show rows in one table that do not match rows in the other table.
A PRACTICAL EXAMPLE
Sound complicated? Let's see an example. I have a table (let's put in on the left) of Estate Agents and on the right a
table of properties for sale.
1. Would give me a table of all the properties for sale through agents, and agent details
2. Would give me the same (all the properties for sale through agents) with an additional line for each agent who's not
got any properties in our list.
3. Would give me just a list of agents with no properties listed
4. Would give me a table of all the properties on our list, whether or not they're for sales through any of the agents
listed in our agent table
5. Would give me just the properties that aren't with one of our agents.
DETAILED THEORY
If you're linking two tables together in MySQL, there are five different types of result that you might want to achieve.
1. You might want to see only those combined (joined) records for which data exists in both tables. That's a regular
join.
2. You might want to see all the combined records, AND in addition an extra record for each row in the left table
which doesn't match any rows at all in the right table; we'll call these extra records in the left table "Orphan rows".
3. You might want to combine the tables so that you can see ONLY the orphan (unmatched) records in the left table.
4. You might want to see all the combined records AND an additional record for each orphan in the right table
5. and finally you might want to see orphan rows from the right table ONLY.
AS MYSQL STATEMENTS
11 |
|
3 | Semington | 222000 | A pleasing detached modern residence
|
12 |
|
3 | Hilperton | 465000 | An individual detached quality home
|
13 |
|
2 | Westbury
| 140000 | Three bedroom semidetached house
|
14 |
|
2 | Trowbridge | 116000 | Larger than average first floor flat
|
15 |
|
11 | Melksham
| 275000 | Grade II Georgian townhouse requiring modernisation |
16 |
-------+------------+--------+-----------------------------------------------------+----+
18 rows in set (0.00 sec)
REGULAR JOIN
| asking |
---------------------------+------------+--------------+------+------+-----+-----------+--------+
| Kavanaghs
| Melksham
| 01225 706860 |
10 |
10 |
1 | Semington
| 225000 |
| Kavanaghs
| Melksham
| 01225 706860 |
10 |
10 |
2 | Melksham
| 195000 |
| Kavanaghs
| Melksham
| 01225 706860 |
10 |
10 |
3 | Atworth
| 237500 |
| Town and Country Estates | Westbury
| 01373 824444 |
8 |
8 |
4 | Westbury
| 152000 |
| Town and Country Estates | Westbury
| 01373 824444 |
8 |
8 |
5 |
Trowbridge | 205000 |
| Halifax
| Melksham
| 01225 703773 |
6 |
6 |
6 | Melksham
| 229950 |
| Halifax
| Trowbridge | 01225 752375 |
5 |
5 |
7 |
Trowbridge | 335000 |
| Alder King
| Trowbridge | 01225 754301 |
1 |
1 | 17 |
Trowbridge | 96950 |
| Halifax
| Trowbridge | 01225 752375 |
5 |
5 |
8 | Hilperton
| 279950 |
| Jayson Kent
| Melksham
| 01225 707798 |
4 |
4 |
9 | Melksham
| 149950 |
| Alder King
| Trowbridge | 01225 754301 |
1 |
1 | 18 |
Trowbridge | 220000 |
| Jayson Kent
| Melksham
| 01225 707798 |
4 |
4 | 10 | Melksham
| 127500 |
| NULL
| NULL
| NULL
| NULL |
11 | 11 | Melksham
| 465000 |
| DK Residential
| Trowbridge | 01225 759123 |
3 |
3 | 12 | Semington
| 222000 |
| DK Residential
| Trowbridge | 01225 759123 |
3 |
3 | 13 | Hilperton
| 465000 |
| Connells
| Trowbridge | 01225 754391 |
2 |
2 | 14 | Westbury
| 140000 |
| Connells
| Trowbridge | 01225 754391 |
2 |
2 | 15 |
Trowbridge | 116000 |
| NULL
| NULL
| NULL
| NULL |
11 | 16 | Melksham
| 275000 |
---------------------------+------------+--------------+------+------+-----+-----------+--------+
18 rows in set (0.00 sec)
AGENTS WITH NO PROPERTIES IN OUR TABLE