tags:

views:

14

answers:

2

I have three tables with a one to many relationship as shown below. What I would like to do is select all the data from "tblOne" based on their relationship to "tblThree".

So without having a query to get all tblTwoIDs I would like to select from tblOne where tblThreeID equals an "Inputed Value"

Hope this makes sense,

Thanks Ben

tblOne

tblOneID

data

tblTwoID


tblTwo

tblTwoID

data

tblThreeID


tblThree

tblThreeID

data

A: 

you need to use join, for example:

SELECT t1.data,t3.data FROM t1,t3 WHERE t1.id = td.id AND id = #
Omry
Its not as simple a join as that though as far as I can see. I have been trying to use a JOIN but with no luck. For instance you are comparing t1.id against an ID but I need to get data from tblOne when they are in a category from table two which is in a series from tableThree.Any suggestions on how the join should look?
Ben
A: 
SELECT tblOne.*  -- use "SELECT DISTINCT tblOne.*" to avoid duplicate results
FROM tblOne
    INNER JOIN tblTwo
        ON tblOne.tblTwoID = tblTwo.tblTwoID
    INNER JOIN tblThree
        ON tblTwo.tblThreeID = tblThree.tblThreeID
WHERE tblTwo.tblThreeID = 42  -- replace "42" with the id you want to find

If you don't need to ensure that the records exist in tblThree then you don't need to join to it at all:

SELECT tblOne.*  -- use "SELECT DISTINCT tblOne.*" to avoid duplicate results
FROM tblOne
    INNER JOIN tblTwo
        ON tblOne.tblTwoID = tblTwo.tblTwoID
WHERE tblTwo.tblThreeID = 42  -- replace "42" with the id you want to find
LukeH
Great, the bottom one worked fine.
Ben