使用row_number和join时的模糊列名

时间:2014-05-09 18:37:56

标签: sql db2

Table_1Table_2都有idname

此查询有效:

SELECT * 
  FROM (SELECT ROW_NUMBER() OVER() AS rn Table_1.*,
               Table_2.*
          FROM Table_1 
          LEFT OUTER JOIN Table_2 on Table_1.id = Table_2.id
       )
  WHERE rn Between 1 AND 10

但是如何在Table_1中选择列名?我用Table_1.name替换了*,它显示:

SQL0206N  "Table_1.name" is not valid in the context where it is used.  
SQLSTATE=42703

2 个答案:

答案 0 :(得分:3)

您似乎错过了rnTable_1之间的逗号。我认为这应该有效:

SELECT * FROM (
    SELECT 
         ROW_NUMBER() OVER() AS rn
        ,Table_1.*
        ,Table_2.*
    FROM Table_1 
    LEFT JOIN Table_2 
      on Table_1.id = Table_2.id
) A
WHERE rn Between 1 AND 10

答案 1 :(得分:0)

正如bhamby所说,看起来你在rnTable_1

之间留下了逗号

您也可以简化查询...

SELECT x.*, y.*
  FROM Table_1 x
  LEFT JOIN Table_2 y
    on x.id = y.id
  FETCH FIRST 10 ROWS ONLY