MYSQL选择逗号后的值

时间:2016-06-03 02:56:34

标签: php mysql

对于mysql来说很新,我有一个文本列。值有两种可能的输入方案:

TIME
hour, second

NAME
second

如何检查值是否具有第一种格式(小时和秒以逗号分隔),如果是,则仅选择“秒”?

2 个答案:

答案 0 :(得分:1)

//Assume that you have two formats only for your input like :

$input = '6, 30';

//or 

$input = '30';

//Maybe you can try to explode your input into array first with comma :

$input_array = explode(",",$input);

//Then you check whether there is second value in your array or not, if yes then take the second value, else take the first one:

$value = count($input_array) > 1 ? $input_array[1] : $input_array[0];

//Here you will always get the right value :

echo $value;

答案 1 :(得分:0)

提示:

select if(locate(',', yourcolumn) > 0, right(yourcolumn, (length(yourcolumn) - locate(',', yourcolumn))), yourcolumn)