我在DB2中使用连接表进行分页时遇到了问题。我想返回包含INNER JOIN的查询的第10-30行。
这有效:
SELECT *
FROM (
SELECT row_number() OVER (ORDER BY U4SLSMN.SLNAME) AS ID,
U4SLSMN.SLNO, U4SLSMN.SLNAME, U4SLSMN.SLLC
FROM U4SLSMN) AS P
WHERE P.ID BETWEEN 10 AND 30
这不起作用:
SELECT *
FROM (
SELECT row_number() OVER (ORDER BY U4SLSMN.SLNAME) AS ID,
U4SLSMN.SLNO, U4SLSMN.SLNAME, U4SLSMN.SLLC, U4CONST.C4NAME
FROM U4SLSMN INNER JOIN U4CONST ON U4SLSMN.SLNO = U4CONST.C4NAME
) AS P
WHERE P.ID BETWEEN 10 AND 30
我得到的错误是:
涉及字段* N。
的选择错误
请注意,JOIN查询本身可以正常工作,而不是在它作为子查询运行时。
如何在DB2中的子查询中执行连接?
答案 0 :(得分:1)
在v7.1 TR9
上工作正常这是我实际运行的内容:
select *
from ( select rownumber() over (order by vvname) as ID, idescr, vvname
from olsdta.ioritemmst
inner join olsdta.vorvendmst on ivndno = vvndno
) as P
where p.id between 10 and 30;
我更喜欢CTE版本:
with p as
( select rownumber() over (order by vvname) as ID, idescr, vvname
from olsdta.ioritemmst
inner join olsdta.vorvendmst on ivndno = vvndno
)
select *
from p
where p.id between 10 and 30;
最后,请注意,在7.1 TR11(7.2 TR3),IBM增加了对LIMIT和OFFSET条款的支持。您的查询可以按如下方式重新完成:
SELECT
U4SLSMN.SLNO, U4SLSMN.SLNAME, U4SLSMN.SLLC, U4CONST.C4NAME
FROM U4SLSMN INNER JOIN U4CONST ON U4SLSMN.SLNO = U4CONST.C4NAME
ORDER BY U4SLSMN.SLNAME
LIMIT 20 OFFSET 9;
然而,请注意LIMIT& OFFSET子句仅在准备或嵌入式SQL中受支持。您无法在STRSQL或STRQMQRY中使用它们。我相信"运行SQL脚本" GUI界面确实支持它们。这是article about LIMIT & OFFSET