Compare Two Strings in MySQL

前端 未结 7 1685
走了就别回头了
走了就别回头了 2021-01-15 17:36

My current query (doesn\'t work)

SELECT url
FROM table
WHERE
    \'http://www.longurl.com/some/string\' like \'%\' . url . \'%\'

table

相关标签:
7条回答
  • this?

    SELECT url
    FROM table
    WHERE
        url like '%http://%longurl.com%'
    

    or you can also use %longurl.com%

    0 讨论(0)
  • 2021-01-15 17:57

    Are you looking for:

    LIKE '%longurl.com%'
    
    0 讨论(0)
  • 2021-01-15 18:15

    If what you need is getting all URLs containing longurl.com then try using:

    SELECT url
    FROM table
    WHERE
    url like '%longurl.com%'
    

    It should work.

    0 讨论(0)
  • 2021-01-15 18:20

    The concatenation operator . does not exist in MySQL, you have to use the CONCAT() function instead:

    SELECT url
    FROM table
    WHERE 'http://www.longurl.com/some/string' LIKE CONCAT('%', url, '%');
    
    0 讨论(0)
  • 2021-01-15 18:20

    What about this:

    SELECT url 
    FROM table
    WHERE url LIKE '%longurl.com%string%'
    

    or, to be just like your PHP:

    SELECT url 
    FROM table
    WHERE url LIKE '%longurl.com%'
    
    0 讨论(0)
  • 2021-01-15 18:22

    Okay, what about:

    SELECT url 
      FROM table 
     WHERE INSTR('http://www.longurl.com/some/string', url) > 0
    

    (Formatting didn't work so well in the comments, so I added it again as an answer.)

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