I have two tables named Evaluation
and Value
.
In both tables, there are four columns. But three of the four are the same. In other words, they both have the CaseNum
, FileNum
, ActivityNum
columns. In addition to those, the Evaluation
table has the Grade
column, and the Value
table has the Score
column.
I want to merge the two into one table, joining by CaseNum
, FileNum
, and ActivityNum
, so I have a new table of five columns, including Value
and Score
.
Can I use INNER JOIN
multiple times to do this?
This question is related to
mysql
sql
sql-server
You would basically want something along the lines of:
SELECT e.*, v.Score
FROM Evaluation e
LEFT JOIN Value v
ON v.CaseNum = e.CaseNum AND
v.FileNum = e.FileNum AND
v.ActivityNum = e.ActivityNum;
You should only need to do a single join:
SELECT e.Grade, v.Score, e.CaseNum, e.FileNum, e.ActivityNum
FROM Evaluation e
INNER JOIN Value v ON e.CaseNum = v.CaseNum AND e.FileNum = v.FileNum AND e.ActivityNum = v.ActivityNum
No, just include the different fields in the "ON" clause of 1 inner join statement:
SELECT * from Evalulation e JOIN Value v ON e.CaseNum = v.CaseNum
AND e.FileNum = v.FileNum AND e.ActivityNum = v.ActivityNum
SELECT E.CaseNum, E.FileNum, E.ActivityNum, E.Grade, V.Score
FROM Evaluation E
INNER JOIN Value V
ON E.CaseNum = V.CaseNum AND E.FileNum = V.FileNum AND E.ActivityNum = V.ActivityNum
Source: Stackoverflow.com