How to retrieve the last 4 characters from a mysql

2020-02-10 03:22发布

问题:

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'

回答1:

The docs have an example directly relevant to this.

select * from table where SUBSTRING(field, -4) = '.png'


回答2:

With RIGHT function :

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

You can achieve the same result with SUBSTRING :

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


回答3:

This Fast query:

Select * from table where picName like '%.png'


回答4:

SELECT * FROM table WHERE SUBSTRING( field, -4 ) = '.png'


标签: php mysql substr