Postgres与AND相似

时间:2016-11-21 12:47:15

标签: postgresql

有没有办法可以将此OR表达式更改为AND?

title SIMILAR TO '%(foo|bar)%'

现在返回标题中包含foo或bar的所有行。如何返回包含foo和bar的行,但不按此顺序返回。

所以“foo bar”和“bar foo”和“foo bla bar”都是有效的结果。但“foo沙拉”不是

2 个答案:

答案 0 :(得分:3)

你可以这样做:

 title SIMILAR TO '%(foo%bar|bar%foo)%'

答案 1 :(得分:3)

将简单all(array[...])with the_data(title) as ( values ('foo bar'), ('bar foo'), ('foo bla bar'), ('foo salad') ) select * from the_data where title like all(array['%foo%', '%bar%']); title ------------- foo bar bar foo foo bla bar (3 rows)

一起使用
with the_data(title) as (
values 
    ('Foo bar'),
    ('Bar foo'),
    ('foo bla bar'),
    ('foo salad'),
    ('foobar'),
    ('barfoo')
)
select *
from the_data
where regexp_split_to_array(lower(title), ' ') @> array['foo', 'bar'];

    title    
-------------
 Foo bar
 Bar foo
 foo bla bar
(3 rows)

您只搜索整个单词的情况有点复杂。您应该使用regex_split_to_array()并检查结果数组是否包含搜索词的数组(使用array operator @>):

<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <h1>Pizza</h1>
        <form method="POST" action="pizza.php">
            Wat op pizza:<br>
            <input type="radio" value="Pepperoni" name="type">Pepperoni<br>
            <input type="radio" value="Ananas" name="type">Ananas<br>
            <input type="radio" value="Ansjovis" name="type">Ansjovis<br>
            <input type="radio" value="Broccoli" name="type">Broccoli<br><br>
            <input type="submit" name="Bestellen" value="Bestellen"><br>
        </form>
    </body>
</html>