We help IT Professionals succeed at work.
Get Started

MySQL Query Array of Counties & Cities

inspirebiz
inspirebiz asked
on
806 Views
Last Modified: 2012-09-14
I've got a database table of zip codes and am grouping zip codes into regions. So I have two tables, one with all of the zip code information and one with the region id and assigned zip code.

I'd like the system to do the following with only one trip to the database. I need the results to display the entire list of zip codes not yet assigned to a region, sorted by county.

So the results will look like this -

County A
12344 - 12345 - 12345 - 12347

County B
22344 - 22345 - 22345 - 22347

Select Distinct County, zipcode from the zip code table where the zip code is not in the regions table.

I'd then like to store these results in an array that I can go through to output the results.

Currently I'm pulling the distinct county, outputting in a foreach loop, and then running a new query to pull the zipcode and for each county.

Is it possible to do this with only one trip to the database? I'm running MySQL and PHP.
Comment
Watch Question
Project Architect
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 10 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE