SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    googlicious graymatter bvarvel's Avatar
    Join Date
    Sep 2002
    Location
    Katy, TX
    Posts
    952
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Selecting Distinct Entries From Table Based on Another

    Let say I have to tables:

    tbl_communities:
    id
    name

    tbl_homes:
    id
    community
    name

    the integer in tbl_homes.community would correspond to tbl_communities.id.

    So how would I draw a query that would pull a distinct list of names BUT ONLY if there is a record in the tbl_homes table.

    I tried this:
    Code:
    SELECT DISTINCT name FROM tbl_communities
    But that pulled names from the table even though there weren't corresponding records in the homes table.

    Can someone please help!!

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,017
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    Code:
    SELECT name
      FROM tbl_communities
     WHERE EXISTS
           ( SELECT 1
               FROM tbl_homes
              WHERE community = tbl_communities.id )
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    googlicious graymatter bvarvel's Avatar
    Join Date
    Sep 2002
    Location
    Katy, TX
    Posts
    952
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bless you!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •