我在generated_code列中有这些值
NEW 6y0m
!EW 0y8m
N!W 5y11m
!EW 17y0m
我想根据位于该空间之后和之前的年份来命令它升序。结果应该是这样的
!EW 17y0m
NEW 6y0m
N!W 5y11m
!EW 0y8m
我试过这个,但它返回空集
select
mid(generated_code,4,LOCATE(generated_code,'y')-6)
from
classifier_bahrain
order by
mid(generated_code,4,LOCATE(generated_code,'y')-6) desc
答案 0 :(得分:2)
locate
函数的参数应该是相反的顺序。此外,如果您尝试按子字符串排序,它将按照6,5,17,0的顺序排列。
因此在订购结果之前将其转换为int。
试试这个:
SELECT generated_code,SUBSTR(generated_code,4,locate('y',generated_code)-4) as ORDERBY
FROM classifier_bahrain
ORDER BY CAST(SUBSTR(generated_code,4,locate('y',generated_code)-4) AS UNSIGNED) DESC
结果:
GENERATED_CODE ORDERBY
!EW 17y0m 17
NEW 6y0m 6
N!W 5y11m 5
!EW 0y8m 0
请参阅SQL Fiddle
中的结果