简体   繁体   English

DB2中的列别名的内部联接

[英]Inner join with column alias in DB2

I have a more complex query with inner join that throws the error: 我有一个更复杂的查询与内部联接抛出错误:

[Error Code: -206, SQL State: 42703] DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703, SQLERRMC=TT.ALIAS, DRIVER=4.13.127. [错误代码:-206,SQL状态:42703] DB2 SQL错误:SQLCODE = -206,SQLSTATE = 42703,SQLERRMC = TT.ALIAS,DRIVER = 4.13.127。 2) [Error Code: -727, SQL State: 56098] DB2 SQL Error: SQLCODE=-727, SQLSTATE=56098, SQLERRMC=2;-206;42703;TT.ALIAS, DRIVER=4.13.127 2)[错误代码:-727,SQL状态:56098] DB2 SQL错误:SQLCODE = -727,SQLSTATE = 56098,SQLERRMC = 2; -206; 42703; TT.ALIAS,DRIVER = 4.13.127

the simple version of the query that doesn't work is: 不起作用的查询的简单版本是:

select column1 as alias from table1 tt
inner join table1 ts
on tt.alias = ts.column1

this simple query doesn't make sense but is a simple version of the query I'm trying to run. 这个简单的查询没有意义,但是我正在尝试运行的查询的简单版本。 This was supposed to run with no errors right? 这应该没有错误运行吗? If I remove the alias it runs with no problem.. 如果我删除它运行的别名没有问题..

Thanks in advance 提前致谢

You cannot use a column alias in an on or where clause. 您不能在onwhere子句中使用列别名。 You can do: 你可以做:

select column1 as alias
from table1 tt inner join
     table1 ts
     on tt.column1 = ts.column1;

You could also do: 你也可以这样做:

select alias
from (select column1 as alias
      from table1
     ) tt inner join
     table1 ts
     on tt.alias = ts.column1;

During the compile phase, the queries are evaluated by clause, with the from clause first, then then where and select . 在编译阶段,查询由子句计算,首先是from子句,然后是whereselect In other words, the compiler doesn't "know" what is in the select when it evaluates the from . 换句话说,编译器在评估from时不会“知道” select的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM