【问题标题】:Postgres is there a way to simplify my where clause without using unnest and string_to_array?Postgres 有没有办法在不使用 unnest 和 string_to_array 的情况下简化我的 where 子句?
【发布时间】:2020-02-16 01:40:42
【问题描述】:

以下 SQL 对我有用,并为我提供了我想要的结果:

select postdomain from post where postdomain not in (select unnest (string_to_array('youtube.com|twitter.com' , '|'))) ;

我想知道是否有一种方法可以简化 where 部分?

例如,如果我不想要完全匹配并且需要不区分大小写的 contains 匹配,那么这个更简单的 SQL 可以在没有 unnest 和 string_to_array 的情况下工作:

select postdomain from post where postdomain !~* 'youtube.com|twitter.com' ;

有没有办法实现类似的完全匹配?

【问题讨论】:

    标签: sql postgresql postgres-9.6


    【解决方案1】:

    不需要unnest,直接使用数组即可:

    select postdomain 
    from post 
    where postdomain <> ALL (string_to_array('youtube.com|twitter.com' , '|')) 
    

    【讨论】:

      【解决方案2】:

      锚定模式:

      where postdomain !~* '^youtube.com$|^twitter.com$'
      

      ^ 匹配字符串的开头,$ 匹配结尾。

      【讨论】:

      • 没有简单的操作符不需要我更改分隔字符串吗?
      • 是的,有;看另一个答案。
      猜你喜欢
      • 2022-10-23
      • 1970-01-01
      • 2011-12-07
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 2016-01-10
      • 2016-01-19
      相关资源
      最近更新 更多