SQL自定义排序

-- 自定义排序
select
     FIND_IN_SET(str, 'a,b,c,d,e') as str_fis
    ,str
from (
    select 'b' as str union all
    select 'a' as str union all
    select 'd' as str union all
    select 'c' as str union all
    select 'f' as str union all
    select 'g' as str union all
    select 'e' as str
) t1
order by str_fis
;

image

注意点

只支持可以穷举出来的,如不在范围内,则为0,会排在最前,不支持排在最后。

posted @ 2025-07-08 14:17  chenzechao  阅读(8)  评论(0)    收藏  举报