【问题标题】:SQL - how to select words with certain values at the end of wordSQL - 如何在单词末尾选择具有某些值的单词
【发布时间】:2013-03-10 15:38:55
【问题描述】:

我是 sql 新手,我想知道如何在名为 Name 的列中找到值末尾的字母或符号? 例如,如果我能找到一些东西,我会写 select * from 'table' where 'Name' like '%es%',但它会发现我所有行都包含 es
可以说 - lesl, pespe, mess... 但是如何编写 select ,它将仅选择带有 'es' 在单词末尾的值? ... 使用 regex 我将使用 es\Z..... 提前致谢!

【问题讨论】:

  • 由于您是新手,在开始实际工作之前,您可能需要学习一些基础知识。为此,我听说过《十分钟内自学 SQL》这本书的好消息。
  • 感谢您的想法。我会找到这本书,并且会越来越好:)

标签: sql select


【解决方案1】:

你必须删除最后一个%,所以它只会选择带有es的词结尾

select * from table where Name like '%es'

【讨论】:

  • 感谢您的回答。这很有帮助:)
【解决方案2】:

您目前正在匹配:..where 'Name' like '%es%'

相当于anything, then 'es' then anything else

删除最后一个 % 会更改要访问的位置 anything then 'es'.

简而言之..你需要..where 'Name' like '%es'

【讨论】:

    【解决方案3】:

    查询 ..where 'Name' like '%es' 将查找名称以“ES”结尾的列。 但是,如果我们需要查找名称以“E”或“S”结尾的列,则查询将是

    .. where 'Name' LIKE '%[ES]'

    【讨论】:

      【解决方案4】:
      1. 如果您想查找以“test”之类开头的名称 使用 => 从表中选择名称,其中名称类似于 'test%'。
      2. 如果您想查找以“test”之类结尾的名称 使用 => 从表中选择名称,其中名称类似于 '%test'。
      3. 如果要查找以 s 开头并以 h 结尾的名称 使用 => select name from table where name like 's%'and name like '%h' 或简单地从表中选择 name where name like 's%h'。

      【讨论】:

        【解决方案5】:

        你也可以使用正则表达式

        select distinct city from station where city REGEXP '[aeiou]$';
        

        资源:To Know more about REGEXP

        【讨论】:

          【解决方案6】:

          使用正则表达式运算符/函数尝试模式 [a-z]ed($)。

          模式说明:

          [a-z] - 匹配任何字母

          es - 匹配 es 字面意思

          ($) - 字符串结尾(所以请确保它后面没有任何字母)

          完整的查询是:

          select * from test 
          where ingr regexp '[a-z]es($)'
          

          【讨论】:

            猜你喜欢
            • 2011-02-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-12-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多