David Sun David Sun - 4 months ago 9
MySQL Question

MySQL: select all the data but if contains ' * ', then show the last four characters

Something like:

SELECT IF( [call_history.callerid contains '*'], [if it contains, keep last four char], [if not, return ' '(not null)] ) AS 'test'

FROM call_history

ORDER BY start DESC;

Answer

I recommend reading the manual page on string functions built into MySQL's flavor of SQL: http://dev.mysql.com/doc/refman/5.7/en/string-functions.html

For example:

SELECT IF(LOCATE(callerid, '*'), SUBSTRING(callerid, -4), '') AS 'test'
FROM call_history
ORDER BY start DESC;