当前位置: 动力学知识库 > 问答 > 编程问答 >

sql - Database left outer join with a base table

问题描述:

I have 3 tables A, B, C with the data as follows

Table A

C1 C2

----------

100 X

200 Y

300 z

Table B

C1 C2

-----------

100 bb

300 bb1

Table C

C1 C2

-----------

200 cc

300 cc1

I want a query which can out put as follows; The count of records in the result should be same as the count of records in A.

100 X 100 bb null null

200 y null null 200 cc

300 z 300 bb1 300 cc1

Note: All the tables have one-one relationship and 3 tables are just given for example. This can be any number of tables.

网友答案:

Try this:

SELECT *
FROM tableA a
LEFT JOIN tableB b ON a.c1 = b.c1
LEFT JOIN tableC c ON a.c1 = c.c1 
网友答案:

try this, should work -

SELECT res.x,
  res.y,
  res.z,
  res.w,
  c.c1,
  c.c2
FROM
  (SELECT a.c1 x, a.c2 y, b.c1 z, b.c2 w FROM a LEFT OUTER JOIN b ON a.c1=b.c1
  ) res
FULL OUTER JOIN c
ON res.x=c.c1
ORDER BY res.x;
分享给朋友:
您可能感兴趣的文章:
随机阅读: