SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,307
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    basic multiple tables select q.

    Real beginner's question I know. Really need an answer ASAP please


    Very standard situation: I have three tables which make up a many-to-many relationship. Table_1 and Table_2 contain the main data. Table_3 contains foreign keys from Table_1 and Table_2 linking them.


    I'm after a SELECT command which selects all Table_1 rows which have links to particular value (say '3') in Table_2.

    command in English:
    select all Table_1 rows which have a Table_2 ref of 3



    Code:
    Table_1
    ref    int    primary key
    ....
    ....
    
    
    Table_2
    ref     int     primary key
    ....
    ....
    
    
    Table_3
    ref            int    primary key
    table_1_ref    int    foreign key to table 1
    table_2_ref    int    foreign key to table 2

    thanks in advance

  2. #2
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,307
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Anyone?... please

    There's enough info there to answer isn't there? I think so.

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,276
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by johnyboy View Post
    command in English:
    select all Table_1 rows which have a Table_2 ref of 3
    Code:
    SELECT table1.*
      FROM table1
    INNER
      JOIN table3
        ON table3.table_1_ref = table1.ref
       AND table3.table_2_ref = 3
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,307
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Many thanks r937 Really appreciated.


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
  •