假设我有这个查询:
select *
from myTable
where myTable.myCol in (1,2,3)
我想这样做:
with allowed_values as (1,2,3)
select *
from myTable
where myTable.myCol in allowed_values
它在第一行给我一个语法错误,你能帮我解决它吗?
假设我有这个查询:
select *
from myTable
where myTable.myCol in (1,2,3)
我想这样做:
with allowed_values as (1,2,3)
select *
from myTable
where myTable.myCol in allowed_values
它在第一行给我一个语法错误,你能帮我解决它吗?
我能想到的最接近你的语法:
WITH allowed_values (id) AS
( VALUES
(1), (2), (3)
)
SELECT *
FROM myTable
WHERE id IN
(TABLE allowed_values) ;
接近你可能想到的:
WITH allowed_values AS (SELECT '{1,2,3}'::int[] AS arr)
SELECT *
FROM my_table
,allowed_values -- cross join with a single row
WHERE my_col = ANY (arr);
更好的:
WITH allowed_values (my_col) AS (VALUES (1), (2), (3))
SELECT *
FROM allowed_values
JOIN my_table USING (my_col)
但实际上,您可以简化:
SELECT *
FROM (VALUES (1), (2), (3)) AS allowed_values (my_col)
JOIN my_table USING (my_col);
尝试
with allowed_values as (select 1 as tst union all select 2 union all select 3)
select * from myTable a
inner join c1 b ON (b.tst = a.myCol)
最简单的方法是更正您的公用表表达式,然后在子选择中使用它。
with allowed_values as (
select 1 id
union all
select 2
union all
select 3
)
select * from myTable
where myTable.id in (select id from allowed_values)