sql查询在列的情况下连接多个表

时间:2014-02-16 00:31:42

标签: sql join

我有这样的SQL:

select case when.....end as colA, table2.col3
from table1
join table2 on table2.colB = colA

在if语句的情况下,还有一些其他的表列,这可行吗?我有错误说colA无效。

1 个答案:

答案 0 :(得分:0)

whereon子句中未理解列别名。

您可以使用子查询执行此操作,假设case仅涉及table1中的列:

select case when.....end as colA, t2.col3
from (select table1.*, (case when.....end) as colA
      from table1
     ) t1 join
     table2 t2
     on t2.colB = t1.colA;

否则,您可以case语句放在on子句中:

select case when.....end as colA, t2.col3
from (select table1.*, (case when.....end) as colA
      from table1
     ) t1 join
     table2 t2
     on t2.colB = ( case when.....end );