I have design problem of database structure. The goal is to have database for offers that our clients offer per some geographical region. Each offer can be offered in many regions.
The regions are in hierarhy - example:
subregion_1
subregion_11
region_111
region_112
subregion_12
region_121
region_122
subregion_2
subregion_21
region_221
Now I want to store in database the offer_1 and regions for that offer. I will give You 3 examples what I have to ahieve:
- when my offer_1 is stored in region_111 then I would like to display this offer when user are browsing the subregion_1, subregion_11 and region_111
- If offer_1 is stored in regions subregion_11 and region_121 then the offer should be displayed when user are browsing the subregion_1, subregion_11 and all branch of subregion_11, subregion_12 and region_121
- when my offer_1 is stored in subregion_1 then the offer is displayed on subregion_1 page and all branch under subregion_1.
Also I have to provide a way to calculate the number of diffrent offers in each regions dynamicaly and very fast. Does somebody have some advice how to aproach this design?
Here is what I have so Far.
Regions
------------------------------------------------------------
| id | level1 | level2 | level3 | name | level |
------------------------------------------------------------
| 02 | 02 | null | null | subregion_1 | 1 |
| 0201 | 02 | 01 | null | subregion_11 | 2 |
| 020103 | 02 | 01 | 03 | region_111 | 3 |
------------------------------------------------------------
Offers to regions
------------------------
| offer_id | region_id |
------------------------
| 1 | 020103 |
| 1 | 0202 |
------------------------
I created id for regions from concatenating level1, level2 and level3. In the table Offers_to_regions I store the offer and the region. Here I have region on level 3 (020103) and region on level 2 (0202) for offer 1. With this design I have problems how to query the numbers of difrent offers per region, and how to query offers for regions on level1, level2 and level3 regions.