简体   繁体   中英

SQL join on multiple levels

I'm struggle with a SQL join for parent client records and just posted a query, but I realised after posting it that my example was slightly wrong (but still a useful post) so i've created a new one, that's more accurate :)

If i have the following database structure:

Table Regions
|Region_no | Region_Level  | owning_region_no |
| 1        |     1         |                  |
| 2        |     2         |          1       |
| 3        |     2         |          1       |
| 4        |     3         |          2       |
| 5        |     3         |          2       |
| 6        |     3         |          3       |

Table Postcodes
| Postcode | Region_no |
| PO32 3AE |     4     |
| PO32 3AA |     5     |
| PO32 3AF |     6     |

Table UnitsMappings
| Unit_No | region_no |
| 1       |     1     |
| 1       |     2     |
| 2       |     2     |
| 2       |     1     |
| 3       |     3     |
| 3       |     3     |
| 4       |     6     |
| 4       |     5     |

Table Units
| Unit_no | Unit_Name  |
|   1     |  South     |
|   2     |  SouthEast |
|   3     |  Central |
|   4     |  SouthWest |

[Updated sample answer] What I really want, is the following:

| Unit_name | Postcode |
| South     | PO32 3AE |
| South     | PO32 3AA |
| South     | PO32 3AF |
| SouthEast | PO32 3AE |
| SouthEast | PO32 3AA |
| SouthEast | PO32 3AF |

Even though South has only regions 1 and 2 mapped to it, the level 3's are mapped to the level 2's and then 1's (and the postcodes are mapped to the level 3's)

Now the difficulty is, that i just want the postcodes (from the postcode tables) which are associated to the level 3 regions levels in regions. So there may be a region level 1 associated to a unit_no, but i need all of the postcodes at the bottom that are mapped to the level 3.

based on your details here's my mapping ,

unit => units_mapping

units_mapping => (Postcodes or regions) // assuming using all table we use postcodes

Postcodes => regions

regions (main)

select reg.region_no, Units.name , pc.postcodes
from regions reg,
     Postcodes pc,
     UnitsMappings UM,
     Units units
where reg.region_no = pc.region_no and 
      pc.region_no = UM.region_no and
      UM.Unit_No = Units.Unit_no
order by reg.region_no asc

if there is null in one table, the record won't appear ...
to handle use inner join

not yet tested ..... just assume :)

There is no valid unit name in you test data for the region in region level 3.

The region with region level 3 are 4, 5 and 6. There is no unit associated with region 4, the unit associated with region 5 and 6 is the unit 4, but in the table unit_name there is no unit 4.

If you have the data then this query will get the data

SELECT pc.postcode, u.unit_name
FROM regions reg
     INNER JOIN Postcodes pc ON reg.region_no = pc.region_no
     INNER JOIN UnitsMappings um ON reg.region_no = um.region_no
     INNER JOIN Units u ON um.unit_no = u.unit_no
WHERE reg.region_level = 3

changing the join on Units from INNER to LEFT will get you the postcodes with NULL as unit_name with the test data.

Try this,

Select u.Unit_name,pc.Postcode from Units as u
inner join UnitsMappings um on u.Unit_no = um.Unit_no
inner join Postcodes as pc on pc.Region_no = um.region_no

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM