db2,带子句,变量和多个连接

时间:2016-10-20 06:35:23

标签: join db2 with-statement

我无法弄清楚如何使用创建和使用变量的漂亮技术,如下例所示:

WITH t(myvar) AS (VALUES('welcome'))
SELECT tablename.*
FROM   tablename, t
WHERE  tablename.column1 = t.myvar

在这样的多连接查询中:

WITH t(myvar) AS (VALUES('welcomeagain'))
SELECT 
    tablename.*,
    joinedtable.valueid
FROM   
    tablename, t?
    left join joinedtable on
    joinedtable.keyid = tablename.keyid
WHERE  
    tablename.column1 = t.myvar

我的意思是,在哪里,如何以及如何加入“t”?环境是DB2 ISeries 7.2。

非常感谢您的帮助

马丁

1 个答案:

答案 0 :(得分:0)

也许这就是你想要的?

WITH t(myvar) AS (VALUES('welcomeagain'))
SELECT 
    tablename.*,
    t.myvar, 
    joinedtable.valueid
FROM   
    tablename
    join t on tablename.column1 = t.myvar
    left join joinedtable on joinedtable.keyid = tablename.keyid

或者

WITH t(myvar) AS (VALUES('welcomeagain'))
SELECT 
    tablename.*,
    t.myvar, 
    joinedtable.valueid
FROM   
    tablename
    left join t on tablename.column1 = t.myvar
    left join joinedtable on joinedtable.keyid = tablename.keyid

或者

WITH t(myvar) AS (VALUES('welcomeagain'))
SELECT 
    tablename.*,
    t.myvar, 
    joinedtable.valueid
FROM   
    tablename
    cross join t 
    left join joinedtable on joinedtable.keyid = tablename.keyid

你不想要的是逗号 - 因为这是旧式连接,并且将它与新样式连接组合的语法总是有点迟钝(如果它可以工作的话。)