通过别名为结果表连接2个表的SQL语法?

时间:2014-03-10 07:42:36

标签: sql join syntax inner-join

我正试图理解SQL。我是否遵循以下逻辑?我可以像这里一样使用括号吗?

-- 1
(table1) inner join (table2) on ( ??? )

-- 2
(table1 as a) inner join (table2 as b) on (a.col1 = b.col1)

-- 3
((select ... from ... where ...) as a)
    inner join
((select ... from ... where ...) as b)
    on (a.col1 = b.col1)

这对我不起作用。

<小时/> 我的查询失败:

((select CREATEDATE, BELEGNRRECH, MNR, UTNR, KTXT
from INFOR.RELFBR
where (SAINT = '90') and (CREATEDATE >= '01.01.14 00:00:00')) as a)
    inner join
((select ANR, MNR from INFOR.RELXDB where (SAINT = '10')) as b)
    on (a.MNR = b.MNR)

-- error message: ORA-00907: missing right parenthesis

我正在使用C#发送SQL查询:

string q1 = "select CREATEDATE, BELEGNRRECH, MNR, UTNR, KTXT from INFOR.RELFBR " +
    "where (SAINT = '90') and (CREATEDATE >= '" + date.ToString("dd.MM.yy HH:mm:ss") + "')";
string q2 = "select ANR, MNR from INFOR.RELXDB where (SAINT = '10')";
string query = "(" + q1 + ") as a inner join (" + q2 + ") as b on (a.MNR = b.MNR)";

// q1 and q2 work, not query

2 个答案:

答案 0 :(得分:0)

inner join
((select ANR, MNR from INFOR.RELXDB where (SAINT = '10')) as b)
    on (a.MNR = b.MNR)

应该是

inner join
(select ANR, MNR from INFOR.RELXDB where (SAINT = '10')) as b
    on (a.MNR = b.MNR)

括号中的选择应具有外部别名,以便在ON

中使用

答案 1 :(得分:0)

试试这段代码,

SELECT RE.CREATEDATE, RE.BELEGNRRECH, RE.MNR,RE.UTNR, RE.KTXT,REL.ANR, REL.MNR FROM 
INFOR.RELFBR RE INNER JOIN INFOR.RELXDB REL  ON RE.MNR = REL.MNR 
WHERE RE.SAINT = '90' AND RE.CREATEDATE >= '01.01.14 00:00:00' AND  REL.SAINT = '10'