select distinct a from table
这是不排序结果
select
distinct a
from
table
order by
decode( a
, '7' , 0
, '1' , 1
, '3' , 2) ;
这是结果没起到自定义排序
```sql
select
distinct a
from
table
order by
decode( a
, '5' , 0
, '1' , 1
, '3' , 2) ;
这时起作用了,是因为自定义排序的内容在当前结果集里有,不能定义当前结果集里不存在的内容去排序
推荐使用这个instr,注意排序内容在一个单引号内
```sql
select
distinct a
from table
order by instr( '1,100,7,5,6,2,3' ,a);

文章探讨了在SQL查询中如何使用DISTINCT关键字结合DECODE和INSTR函数进行自定义排序。DECODE函数在某些情况下可能无法按预期工作,而INSTR则提供了一种确保排序顺序的方法,特别是当排序依据的值存在于给定的字符串中时。
1704

被折叠的 条评论
为什么被折叠?



