Mysql: Find rows where column value ends with a specific substring

前端 未结 4 1261
情书的邮戳
情书的邮戳 2021-02-18 19:20

I have a column url where all the values are urls. I\'m trying to update the value of a different column where-ever the aforementioned url ends with .pdf

相关标签:
4条回答
  • 2021-02-18 19:41

    I might be oversimplifying... but based on your description, would LIKE solve your problem?

    UPDATE YourTable
    SET DifferentColumn = 'NewValue'
    WHERE Url LIKE '%.pdf'
    
    0 讨论(0)
  • 2021-02-18 19:42

    So it looks like you are looking for the like parameter.

    for example:

    SELECT column(s) FROM table WHERE URL LIKE '%.pdf'
    

    Or you can update with

    UPDATE table SET column = value, ..., WHERE URL LIKE '%.pdf'
    

    The % is like saying anything before the .pdf is htis case.

    Hope this helps.

    0 讨论(0)
  • 2021-02-18 19:44

    You can use a LIKE condition with a leading wildcard. %.pdf means the column must end in .pdf

    UPDATE mytable
    SET newcolumn = 'PDF found'
    WHERE yourcolumn LIKE '%.pdf'
    

    Alternatively you could use the right() function

    UPDATE mytable
    SET newcolumn = 'PDF found'
    WHERE right(yourcolumn,4) = '.pdf'
    
    0 讨论(0)
  • 2021-02-18 19:51

    In your WHERE clause, use something like LOCATE('.pdf',url) > 0 to identify the record of interest.

    As pointed out, this will give you any url that contains '.pdf' in the string...

    If you definitely want the '.pdf' to be at the end, you can also try:

    LOWER(RIGHT(url,4)) = '.pdf'

    I would use LOWER or UPPER to deal with any case issues.

    You can also use LIKE %.pdf (again, watch out for upper/lower case issues) as suggested by others.

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