我有桌子A(ida PK,entry_year, . . .)
, B(idc PK,ida FK,value1, . . .)
and C(year PK,value2, . . .)
我试图从表 B 中选择第一个 n value1(与表 A 的特定 ida 相关),其中 n 是表 C 的 value2,其 C.year 与 A.entry_year 相同。该代码与此类似:
select value1 from A as Alpha join B using(ida)
where A.ida=$1
limit (select value2 from C where year=(select entry_year from A where A.ida=Alpha.ida))
但我收到以下错误: LIMIT 的参数不得包含变量。
我应该做些什么?
thanks!
您可以使用行号 https://www.postgresql.org/docs/current/static/functions-window.html模拟 LIMIT。
SELECT * FROM (
select
value1,
entry_year,
row_number() OVER() AS rownum --Probably you should use PARTITION BY here using student id if you want to retrieve more than one student
from
A as Alpha
join B using(ida)
where A.ida=$1) as tmp
JOIN C ON (C.year = tmp.entry_year)
WHERE rownum <= C.value2
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)