Inverse of SQL LIKE '%value%'

后端 未结 3 716
眼角桃花
眼角桃花 2021-02-04 19:48

I have a MySQL table containing domain names:

+----+---------------+
| id | domain        |
+----+---------------+
|  1 | amazon.com    |
|  2 | google.com    |
         


        
相关标签:
3条回答
  • 2021-02-04 20:09

    You can use the column on the right of the like too:

    SELECT domain FROM table WHERE 'www.google.com' LIKE CONCAT('%', domain);
    

    or

    SELECT domain FROM table WHERE 'www.google.com' LIKE CONCAT('%', domain, '%');
    

    It's not particularly efficient but it works.

    0 讨论(0)
  • 2021-02-04 20:11

    You could use a bit of SQL string manipulation to generate the equivalent of string.EndsWith():

    SELECT * FROM table WHERE  
    substring('www.google.com',
    len('www.google.com') - len([domain]) ,
    len([domain])+1) = [domain]
    
    0 讨论(0)
  • 2021-02-04 20:16

    In mysql you can use regular expressions (RLIKE) to perform matches. Given this ability you could do something like this:

    SELECT * FROM table WHERE 'www.google.com' RLIKE domain;
    

    It appears that the way RLIKE has been implemented it is even smart enough to treat the dot in that field (normally a wildcard in regex) as a literal dot.

    MySQL's inclusion of regular expressions gives you a very powerful ability to parse and search strings. If you would like to know more about regular expressions, just google "regex". You can also use one of these links:

    http://en.wikipedia.org/wiki/Regular_expression

    http://www.regular-expressions.info/

    http://www.codeproject.com/KB/string/re.aspx

    0 讨论(0)
提交回复
热议问题