Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
producing a query which will give me unique city values based on the state
Old 01-16-2009, 12:33 PM producing a query which will give me unique city values based on the state
Inet411's Avatar
Skilled Talker

Posts: 88
Name: programmer
Location: internet
Trades: 0
I need help producing a query which will give me unique city values based on the state.

Sample Structure.

id|state|city|zip

1|CO|Denver|77201
2|CO|Denver|77202
3|CO|Denver|77203
4|IL|Chicago|60505
5|IL|Chicago|60504
6|IL|Springfield|61202
7|IL|Joliet|65243
8|CO|Arvada|77302
9|IN|Springfield|50309
10|IN|Hamilton|50398
11|DE|Denver|10348


Desired results:
CO Denver
IL Chicago
IL Springfield
IL Joliet
CO Arvada
IN Springfield
IN Hamilton
DE Denver

What I have tried so far.
SELECT DISTINCT(city) FROM states.
That gives me:
CO|Denver|77201
IL|Chicago|60505
IL|Springfield|61202
IL|Joliet|65243
CO|Arvada|77302
IN|Hamilton|50398

As you can see it removed DE's denver, I only want it to remove duplicate cities for each state not duplicate cities for the entire US.

Also tried using GROUP BY city same result.
Also tried GROUP BY state and that only gives one result per state.

Using php I can just pull all the results and and remove the duplicates. This gives me the desired results but my database has nearly 50000 records so it takes forever.
If there is a simple query I can do that would be great. Any help or ideas would be greatly appreciated.
__________________

Please login or register to view this content. Registration is FREE

Inet411 is offline
Reply With Quote
View Public Profile Visit Inet411's homepage!
 
 
Register now for full access!
Old 01-17-2009, 09:45 AM Re: producing a query which will give me unique city values based on the state
Zycron's Avatar
Novice Talker

Posts: 10
Name: Keith
Trades: 0
I used a table with the same sample data and was able to get
Code:
SELECT DISTINCT(state), city FROM states
to work.
__________________

Please login or register to view this content. Registration is FREE
Zycron is offline
Reply With Quote
View Public Profile Visit Zycron's homepage!
 
Old 01-20-2009, 10:03 AM Re: producing a query which will give me unique city values based on the state
anderswc's Avatar
Super Talker

Posts: 132
Name: Will Anderson
Location: Terre Haute, IN
Trades: 0
Frankly I would use two tables to store this anyway, to reduce the storage size.

Code:
id|state|city

1 |CO   |Denver
4 |IL   |Chicago
6 |IL   |Springfield
7 |IL   |Joliet
8 |CO   |Arvada
9 |IN   |Springfield
10|IN   |Hamilton
11|DE   |Denver
Code:
id|zip

1 |77201
1 |77202
1 |77203
4 |60505
4 |60504
6 |61202
7 |65243
8 |77302
9 |50309
10|50398
11|10348
Now not only is your query easier, but you don't store as much duplicate data.
Now to add a new zip code, just add the id and zip to the second table, rather than the id,state,city, and zip to the first table.
__________________
Will Anderson

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by anderswc; 01-20-2009 at 10:05 AM..
anderswc is offline
Reply With Quote
View Public Profile Visit anderswc's homepage!
 
Reply     « Reply to producing a query which will give me unique city values based on the state
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 4.88782 seconds with 12 queries