Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 15th, 2003, 03:41 PM
nothinfacd nothinfacd is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 40 nothinfacd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 7 m 57 sec
Reputation Power: 6
combining 2 select statements

My table consists of the City, State, and Zip

For example:
City State Zip
Apple MD 06055
Laurel MD 65645
Criskill VA 85454

I have a $zipcode variable passed in by a query string. I want to find the State that matches that zipcode, then select/print all rows that are in that State.

I believe it's something like this, but it doesn't work


PHP Code:
 SELECT State as thisState FROM citystatezip
WHERE Zip 
'65645'
UNION (
SELECT City
FROM citystatezip
WHERE State 
=  thisState )
ORDER  BY City 



Thanks in advance

John

Reply With Quote
  #2  
Old October 15th, 2003, 09:45 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,982 r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 18 h 20 m 26 sec
Reputation Power: 1028
Code:
select City, State, Zip 
  from citystatezip
 where State IN 
       ( select distinct State
           from citystatezip
          where Zip = '65645' )
order
    by City

you could use an equal sign where i have IN, but i prefer IN because it is "defensive SQL" and will not crap out in the admittedly minuscule chance that there are two states with the same Zip -- not really anything to worry about in this case, i guess, but a coding strategy to keep in mind

rudy
http://r937.com/

Reply With Quote
  #3  
Old October 16th, 2003, 09:24 AM
nothinfacd nothinfacd is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 40 nothinfacd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 7 m 57 sec
Reputation Power: 6
Hey thanks for the quick response. I tried what you said but got the following error

MySQL said:

You have an error in your SQL syntax near 'SELECT DISTINCT State
FROM citystatezip
WHERE Zip = '$thezip' )
ORDER
BY City L' at line 5


Any idea what it could be?

John

Reply With Quote
  #4  
Old October 16th, 2003, 09:37 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,982 r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 18 h 20 m 26 sec
Reputation Power: 1028
yeah, i know what it could be

it could be that you didn't post in the MySQL forum, you posted in the General Database Discussion forum, and forgot to mention that you were using a database that doesn't support subselects



you will have to do your query in two steps

run one query to get the state(s) corresponding to the selected zip, then run a second query to get the cities


rudy

Reply With Quote
  #5  
Old October 17th, 2003, 08:39 AM
nothinfacd nothinfacd is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 40 nothinfacd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 7 m 57 sec
Reputation Power: 6
ok great thanks for your help!



john

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > combining 2 select statements


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT