SELECT * FROM user_follow WHERE (post, "user", type, following)
IN ((25, 1, 'pc' || 'pl' || 'pf', true), (25, 2, 'pc' || 'cc'|| 'cl', true))
在第三列中,我想匹配“ pc”,“ pl”,“ pf”。我不相信我在使用正确的语法,因为我什么时候都没有得到结果,而且我也尝试使用OR而不是||。但它也不起作用。正确的方法是什么?
我仍然需要多个IN
查询,它们要与内括号中的确切要求匹配
答案 0 :(得分:0)
||
进行字符串连接。
看起来像你想要的
SELECT * FROM user_follow
WHERE post=25
AND (
("user" = 1 AND type IN ('pc', 'pl', 'pf'))
OR ("user" = 2 AND type IN ('pc', 'cc', 'cl'))
)
AND following=true
您还可以这样定义可能的组合:
with params(u, t) as values (
(1, array['pc', 'pl', 'pf']),
(2, array['pc', 'cc', 'cl'])
)
select "user".* from "user", params
where post=25
and "user"=u
and type=any(t)
and following=true
答案 1 :(得分:0)
好吧,您始终可以使用普通的逻辑运算符来重写它,诸如此类...
SELECT
*
FROM
user_follow
WHERE
(
post = 25,
AND "user" = 1
AND type IN ('pc', 'pl', 'pf')
AND = true
)
OR (
post = 25,
AND "user" = 2
AND type IN ('pc', 'cc', 'cl')
AND = true
)
答案 2 :(得分:0)
您可以如下查询:
SELECT *
FROM user_follow
WHERE ("user" IN ('pc', 'pl', 'pf') AND (post, type, following) IN ((25, 1, true)
OR "user" IN ('pc', 'cc', 'cl') AND (post, type, following) IN ((25, 2, true));
让我知道它是否满足您的要求。
谢谢, Idrees