答案 0 :(得分:5)
答案 1 :(得分:2)
您也可以通过从字符串中删除A-
来获取最大值。
查询
select max(`t`.`num`) as `maxnb` from (
select cast(replace(`no_box`, 'A-', '') as signed) as `num`
from `tbl_pengecekan`
) as `t`;
另一种解决方法是,先按长度的降序对列进行排序,然后再按列本身的降序进行排序。然后将结果限制为1。
查询
select replace(`no_box`, 'A-', '') as `maxnb`
from `tbl_pengecekan`
order by length(`no_box`) desc, `no_box` desc limit 1;
答案 2 :(得分:1)
您获得的值为9
,因为这是查询集中的 lexigraphical 最大值。如果要获得数字最大值,则必须首先将这些值cast转换为整数(基本上从string
转换为int
)。您可以使用cast(substr(no_box, 3) as signed)
完整代码
SELECT
max(cast(substr(no_box, 3) as signed)) as maxnb
FROM tbl_pengecekan