我必须执行大量的查询,其中相同的值被重用。我想到了类似这样的东西:
varName = 'value';
select * from sometable t where
t.field1 = varName
or t.field2 = varName;
如何在PostgreSQL 12中做到这一点?
我试了很多我找到的东西,但似乎都不起作用。
发布于 2020-12-11 20:04:56
或者,您可以在CTE中使用VALUES
。这将使您能够在同一“变量”中有多个值。
数据示例:
CREATE TABLE t (c1 int, c2 text);
INSERT INTO t VALUES (42,'foo'),(1,'xpto');
查询
WITH j (var) AS (VALUES ('foo'),('bar'))
SELECT t.c1,j.var FROM t
JOIN j ON j.var = t.c2;
c1 | var
----+-----
42 | foo
发布于 2020-12-11 19:50:37
同时我找到了一个解决方案
with varName as (select 'value'::text)
select * from sometable t where
t.field1 = (select * from varName)
or t.field2 = (select * from varName);
https://stackoverflow.com/questions/65250382
复制相似问题