How to retrieve the last 4 characters from a mysql database field?

前端 未结 4 1252
面向向阳花
面向向阳花 2020-12-16 12:07

In order to be able to simulate substr from PHP and mysql, I want to do something like

select * from table where last_four_chars(field) = \'.png\'

相关标签:
4条回答
  • 2020-12-16 12:40

    This Fast query:

    Select * from table where picName like '%.png'
    
    0 讨论(0)
  • 2020-12-16 12:43

    With RIGHT function :

    SELECT RIGHT('abcdefg', 3);
    -- efg
    

    You can achieve the same result with SUBSTRING :

    SELECT SUBSTRING('abcdefg', -3);
    -- efg
    
    0 讨论(0)
  • 2020-12-16 12:49

    The docs have an example directly relevant to this.

    select * from table where SUBSTRING(field, -4) = '.png'
    
    0 讨论(0)
  • 2020-12-16 12:53
    SELECT * FROM table WHERE SUBSTRING( field, -4 ) = '.png'
    
    0 讨论(0)
提交回复
热议问题