【问题标题】:How to use regex in SQL which will find only one row如何在 SQL 中使用正则表达式,它只会找到一行
【发布时间】:2020-04-07 18:44:38
【问题描述】:

我有一个有 3 行的表,我想使用 like 语句只获取第一行。当我使用select value from table where value like '%Motor%%Fans%%9%' 时,它返回两行第一行和第二行。如何排除这些在 9 之前具有不同数值的行?

VALUE
Motor Fans Base 9
Motor Fans Base 49
Motor Fans Base 13

【问题讨论】:

    标签: sql oracle where-clause sql-like


    【解决方案1】:

    您可以使用like。只需在最后的9 之前使用一个空格并删除最后的%

    where value like '%Motor%Fans% 9'
    

    如果您还想匹配以 91 或 903 结尾的值,那么最后的通配符是合适的:

    where value like '%Motor%Fans% 9%'
    

    【讨论】:

      【解决方案2】:

      你可以使用regexp_like():

      where regexp_like(value, 'Motor.*Fans\D*9')
      

      正则表达式分解:

      Motor         # litteral string "Motor"
      .*            # 0 to n characters
      Fans          # litteral string "Fans"
      \D*           # 0 to n non-digit characters
      9             # a litteral "9"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-18
        • 1970-01-01
        • 1970-01-01
        • 2012-01-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多