Reputation: 635
I have two tables and a third to link the records. I am trying to make a query that will select t1.name and t2.value where their ID's are tied in table 3. It has been so long since I have done any sql I cannot remember how to do this properly,
Table1
-------------
ID Name
1 bill
2 bob
3 ben
4 steve
Table2
ID Value
--------------
1 blue
2 red
3 green
4 orange
Table3
-----------------------------
ID Table1ID Table2ID
1 4 2
2 3 1
3 2 4
4 1 3
Upvotes: 2
Views: 8437
Reputation: 82474
It's a simple INNER JOIN
.
Might be quicker to get an answer by searching sql tutorials then by asking here.
SELECT Table1.Name, Table2.Name
FROM Table1
INNER JOIN Table3 ON Table1.Id = Table3.Table1Id
INNER JOIN Table2 ON Table3.Table2Id = Table2.Id
Upvotes: 5
Reputation: 12754
The query should be something like this for example in MySQL dialect.
SELECT t1.Name, t2.Value FROM Table1 t1
JOIN Table3 t3 ON t3.Table1ID = t1.ID
JOIN Table2 t2 ON t3.Table2ID = t2.ID;
However, note that there can be several variants, and the best for you depends on your exact database specification (for example LEFT JOIN or INNER JOIN, etc.)
Upvotes: 0