|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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:
Thanks in advance John |
|
#2
|
||||
|
||||
|
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/ |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
||||
|
||||
|
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 |
|
#5
|
|||
|
|||
|
ok great thanks for your help!
john |
![]() |
| Viewing: Dev Shed Forums > Databases > Database Management > combining 2 select statements |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|