"Jumping" 2 tables in a single SELECT statement

  • 'lo

    Consider this:

    Table A

    - ID

    - Name

    Table B

    - ID

    - AID (FK for A.ID)

    - Name

    Table C

    - ID

    - BID (FK for B.ID)

    - Name

    I'm wanting to return rows from Table C. No problem:

    SELECT C.* FROM C WHERE C.ID = @ID

    I'm also wanting to return the B.Name for the FK B.ID; again, no problem:

    SELECT C.*, B.Name FROM C, B WHERE C.ID = @ID AND B.ID = C.BID

    I'm also wanting to return A.Name for the FK B.AID... problem:

    How the heck do I do that?

    I'm wanting the returned table to have these fields:

    C.ID | C.BID | C.Name | B.Name | B.AID | A.Name

    So... how?

  • Just a join, right?

    select c.id, c.bid, c.name, b.name, b.aid, a.name

    from c

    inner join b on c.bid=b.id

    inner join a on a.id=b.aid

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply