关于Select Where In 的排序问题
程序员文章站
2023-11-13 14:12:52
复制代码 代码如下:select * from table1 &nbs...
复制代码 代码如下:
select *
from table1
where (id in (3,5,1,4,2))
复制代码 代码如下:
select *
from table1
where (id in (3,5,1,4,2))如果in后面的条件都是数字,那mssql排序应该为
复制代码 代码如下:
select *
from table1
where (id in (3,5,1,4,2))
order by charindex(',' + convert(nvarchar, id) + ',', ',' + convert(nvarchar, replace('3,5,1,4,2', ' ','')) + ',')
复制代码 代码如下:
select *
from table1
where (id in (3,5,1,4,2))
order by charindex(',' + convert(nvarchar, id) + ',', ',' + convert(nvarchar, replace('3,5,1,4,2', ' ','')) + ',')改进方法,不去处理空格,直接改用空格判断.可以用来判断少数有空格的字符条件.
复制代码 代码如下:
select *
from table1
where (id in (3,5,1,4,2))
order by patindex('% ' + convert(nvarchar(4000), id) + ' %', ' ' + convert(nvarchar(4000), replace('3,5,1,4,2', ',',' , ')) + ' ')
复制代码 代码如下:
select *
from table1
where (id in (3,5,1,4,2))
order by patindex('% ' + convert(nvarchar(4000), id) + ' %', ' ' + convert(nvarchar(4000), replace('3,5,1,4,2', ',',' , ')) + ' ')
其实还是在sql外处理好条件字符串再进行查询和排序比较好.
对于mysql排序可能要改为(mysql不熟):
复制代码 代码如下:
select *
from table1
where (id in (3,5,1,4,2))
order by find_in_set(id, '3,5,1,4,2')
上一篇: python机器学习简介