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

时间:2016-07-11 21:57:50

标签: mysql sql if-statement subquery oracle-sqldeveloper

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;

1 个答案:

答案 0 :(得分:0)

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;