我想转换一个由实际值组成的IN子句,而不是使用join子句转换子查询。
select max(c1) from t1 where t1.id IN (
1,2,3,4......1000
)
and t1.c2 = something
如何将此查询转换为连接子句?
您可以使用内联VALUES
构建数据集,并使用别名来提供该数据集以及名称和列名称。
SELECT
yourData.id, map.b
FROM
yourData
INNER JOIN
(
VALUES
(1,2),
(2,4),
(3,8)
)
map(a,b)
ON map.a = yourData.a
如果序列线性增加,请使用generate_series
select max(c1) from t1
join generate_series(1, 1000) ids on t1.id = ids
where t1.c2 = something
你可以尝试如下
with cte as
(
select 1 as col union all
select 2 union all
select 3 union all
select 4 union all
---------
-------
select 1000
) select max(t1.c1) from
cte join t1 on cte.col=t1.id where t1.c2=something