Results 1 to 7 of 7

Thread: [RESOLVED] select statement to filter one table on another

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Resolved [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

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    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'

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: select statement to filter one table on another

    Not only that, but it returns then all twice. Bleh.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    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

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: select statement to filter one table on another

    select distinct?

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Re: select statement to filter one table on another

    Quote 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
  •  



Click Here to Expand Forum to Full Width