PostgreSQL加入多个表

时间:2017-08-06 19:17:29

标签: sql postgresql

大家好我想在这里加入表,我想添加一个名为Table2的表,以便显示该表中的学生。令人困惑的是我已经加入了这个查询,所以应该如何添加另一个连接,以便显示来自Table2的学生,而不会破坏现有的代码。另外,我无法检查输出

ID可变数据是stTable 1中的stsidno,表2中的sidno,表schedprd中的scsidno

以下是代码:

                String selS = "select distinct stdistrict,stschool,stsidno,"
                  + " sname as name,stgrade,"
                  + "S.recnum as recnum, S.stldate as stldate,scsec,sctea,sccor,scgrade,scclsprd,scgrdprd,"
                  + "case when P.scchangestartdate is null then C.clstart else "
                  + "P.scchangestartdate end as scchangestartdate, "
                  + "case when S.stedate is null or S.stedate<C.clstart "
                  + "then C.clstart else S.stedate end as stedate "
                  + "from stTable1 as S join schedprd as P on "
                  + "(scyear=styear and scdistrict=stdistrict and scschool=stschool "
                  + "and stsidno=scsidno and (scsec is not null and scsec<>'')) "
                  + "left outer join calendar as C on (C.clyear=S.styear and "
                  + "C.cldistrict=S.stdistrict and C.clschool=S.stschool and C.cltype='10') "
                  + "where styear=? and stdistrict=? ";

1 个答案:

答案 0 :(得分:0)

您可以UNION将{1}} Table1和Table2放在一起,然后在查询中引用CTE而不是Table1,如下所示:

Common Table Expression (CTE)