Reputation: 63
I have a SQL like this:
SELECT
A.NAME,
A.CODE,
B.b_grade,C.c_grade
FROM A
JOIN B ON B.NAME = A.NAME
JOIN C ON C.NAME = A.NAME
I want to get the result like the Veen show. but the SQL's result give me another.
PS:the table B don’t have column “c_grade” ,so at the sql result ,for B ,the column “c_grade ” can be set zero
Upvotes: 0
Views: 88
Reputation: 1154
Try this
SELECT *
FROM tabA
INNER JOIN tabB
ON tabA.primaryKey=tabB.tabAId
INNER JOIN tabC
ON tabA.primaryKey=tableC.tabAId
Upvotes: -1
Reputation: 1105
Below is all you need to do.
SELECT A.NAME, A.CODE, B.B_GRADE, NULL AS C_GRADE
FROM A JOIN B ON B.NAME = A.NAME
UNION ALL
SELECT A.NAME, A.CODE, NULL AS B_GRADE, C.C_GRADE
FROM A JOIN C ON C.NAME = A.NAME
Upvotes: 1
Reputation: 8163
You can left outer join, and then check whether any of the tables matched:
SELECT
A.NAME,
A.CODE,
B.*,C.*
FROM A
LEFT JOIN B ON B.NAME = A.NAME
LEFT JOIN C ON C.NAME = A.NAME
WHERE B.NAME != NULL OR C.NAME != NULL
Upvotes: 1