简体   繁体   English

Postgresql如何使用IN进行多列文本匹配

[英]Postgresql how to use IN for multiple column text match

SELECT * FROM user_follow WHERE (post, "user", type, following)
IN ((25, 1, 'pc' || 'pl' || 'pf', true), (25, 2, 'pc' || 'cc'|| 'cl', true))

In the third column I would like to match either 'pc', 'pl', 'pf'. 在第三列中,我想匹配“ pc”,“ pl”,“ pf”。 I do not believe I am using right syntax as I get no result when I should and also I have tried using OR instead of || 我不相信我在使用正确的语法,因为我什么时候都没有得到结果,而且我也尝试使用OR而不是||。 but it also does not work. 但它也不起作用。 What's the correct way of doing this? 正确的做法是什么?

I still need the multiple IN query that matches the exact requirements in the inner brackets 我仍然需要与内括号中的确切要求匹配的多个IN查询

|| does string concatenation. 进行字符串连接。

It looks like you want 看起来像你想要的

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

You could also define the possible combinations like this: 您还可以这样定义可能的组合:

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

Well, you can always rewrite it by using plain logical operators, something like this... 好吧,您总是可以使用普通的逻辑运算符来重写它,就像这样...

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
    )

You can query like below: 您可以如下查询:

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));

Let me know if it fulfils your requirement. 让我知道它是否满足您的要求。

Thanks, Idrees 谢谢,Idrees

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM