如何从具有任何列出条款的MySQL中选择记录

时间:2017-01-22 13:25:42

标签: mysql sql phpmyadmin

我希望从MySQL记录中选择(或更新)某些列x具有以下任何一个术语:

introduction  
primer  
first course  
fundamentals  
essentials  
for Dummies 

x的数据类型是varchar。

大多数都是单字词。只要它们出现在x中的字符串中,就应该返回(或更新)。其中一些是多字词。在这种情况下,所有单词必须一起出现并以相同的顺序出现在字符串中的任何位置。

如何构建此SQL查询? (或者我如何使用phpMyAdmin GUI获得相同的结果?)

3 个答案:

答案 0 :(得分:0)

使用regexp

where x regexp 'introduction|primer|first course|fundamentals|essentials|for Dummies'

另一种方法是使用like和一堆or s:

where x like '%introduction%' or
      x like '%primer%' or
      . . .

答案 1 :(得分:0)

这是答案。

    UPDATE MyTable

    SET StringColumn = REPLACE (StringColumn, 'SearchForThis', 'ReplaceWithThis')

    WHERE SomeOtherColumn LIKE '%PATTERN%'

答案 2 :(得分:0)

select  *
from    mytable       
where   rec regexp '[[:<:]](introduction|primer|first course|fundamentals|essentials|for Dummies)[[:>:]]'
  

[[:&lt;:]],[[:&gt;:]]

     

这些标记代表字边界。他们匹配开头和   词尾,分别。一个单词是一系列单词字符   不在单词字符之前或之后的单词字符。一个字   character是alnum类中的字母数字字符或   下划线(_)。

http://dev.mysql.com/doc/refman/5.7/en/regexp.html