|
-
Mar 13th, 2007, 06:45 AM
#1
Thread Starter
PowerPoster
[RESOLVED] select statement to filter one table on another
Suppose I have two tables ...
TABLE A
name type state city
john a tx houston
john b ms jackson
john a ny new york
bill a tx houston
bill b mi grand rapids
bill c ill chicago
al b ms jackson
TABLE B
name address
john 123 new st
bill 456 old st
al 789 mid st
and i want to query all of the table b info that meets certain criteria in table a.
examples:
1) all table b entries where person is assigned the city Houston would return:
name address
john 123 new st
bill 456 old st
1) all table b entries where person is assigned any type b would return:
name address
john 123 new st
bill 456 old s
etc etc for any combination of type city state values.
what would the select statement look like? At first I thought this would be trivial and IVe been playing with Joins but so far am not even close ... any help greatly appreciated!
Last edited by Muddy; Mar 14th, 2007 at 06:38 AM.
Reason: resolved
-
Mar 13th, 2007, 07:02 AM
#2
Re: select statement to filter one table on another
select b.* from a, b where a.city = 'Houston'
I hope your real schematic identifiers are a little more descriptive than that.
Edit: don't try this one, try the one below.
Last edited by penagate; Mar 13th, 2007 at 10:17 AM.
-
Mar 13th, 2007, 10:07 AM
#3
Re: select statement to filter one table on another
That query will return every record from B several times.
You must Join the two tables
select b.*
from b inner join a on a.name = b.name
where a.city = 'Houston'
-
Mar 13th, 2007, 10:17 AM
#4
Re: select statement to filter one table on another
Not only that, but it returns then all twice. Bleh.
-
Mar 13th, 2007, 10:24 AM
#5
Thread Starter
PowerPoster
Re: select statement to filter one table on another
Thanks guys! Bruce your select statement gets me closer to what I need (thanks!!), but I still have a problem. If the tables are as below I get 2 identical records returned and I only want one. I understand why it is working like this, but I still cant seem to figure out how to fix it.
Any ideas on how to fix this?
TABLE A
name type state city
john a tx houston
john b tx houston
john b ms jackson
john a ny new york
bill a tx houston
bill b mi grand rapids
bill c ill chicago
al b ms jackson
TABLE B
name address
john 123 new st
bill 456 old st
al 789 mid st
-
Mar 13th, 2007, 10:27 AM
#6
Re: select statement to filter one table on another
-
Mar 13th, 2007, 10:48 AM
#7
Thread Starter
PowerPoster
Re: select statement to filter one table on another
 Originally Posted by penagate
select distinct?
Thanks! that seems to work ...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|