Wednesday, February 1, 2012

MySQL Query Design. Need some thoughts on how I can go about searching a service area with 2 tables.?

I have two tables within a database.

Table 1 has City, County, State, Zip Code (that contains the entire US)

Table 2 has Attorney Name, Firm name,....City, State, County, Zip Code, County 1, County 2, County3, County 4 (Which are the attorney's service area - the 4 counties)



I want to create a MySQL SELECT query to search for attorneys by entering a zip code. But the results, I would like it to show ALL zip codes within the county that I entered by zipcode. If I chose 60601 (which is Chicago, IL Cook County) then I would like it to show all the attorneys within Cook county with all the zip codes within that county such as 60601 60602 60603 60604 60605 etc...



Therefore, Select 60601 from attorney but show ALL attorneys with the zip codes 60601 60602 60603 60604 60605 etc...MySQL Query Design. Need some thoughts on how I can go about searching a service area with 2 tables.?
It is better to normalise your DB (table 2)...



If not, you will need to join table 2 to table 1 four times (use aliases) - each of service counties in table 2 to county in table 1.



With normalised DB you will need only one join

No comments:

Post a Comment