sql语句中怎么按照in语句里的id原有的顺序进行排序输出
可以做成浏览记录、查看、观看记录,视频浏览记录、商品浏览记录
有时候我们需要按照in条件里的id顺序输出结果,可sql语句在不加order by的时候是按照asc排序的,下边的sql解决按照in条件顺序的id输出查询结果
Access:(测试可以使用)
select * From 表 Where id in(2,9,4,3,6,8,1,7,5) order by instr(',2,9,4,3,6,8,1,7,5,',','&id&',')
MSSQL:(测试可以使用)
select * From 表 Where id in(2,9,4,3,6,8,1,7,5) order by charindex(','+rtrim(cast(id as varchar(10)))+',',',2,9,4,3,6,8,1,7,5,')
MySQL:
select * From 表 Where id in (2,9,4,3,6,8,1,7,5) order by instr(',2,9,4,3,6,8,1,7,5,',',CONCAT(',',id,','))
自定义浏览记录排序的处理函数:
'无规则排序,浏览记录排序方式
function qwbm_sqlorder(p_xuid,xml1)
pai_torder = ""
if qdatatype="sqlserver" then
pai_torder = " order by charindex(','+rtrim(cast(id as varchar(10)))+',',',"& p_xuid &",')"
else
pai_torder = " order by instr(',"& p_xuid &",',','&id&',')"
end if
qwbm_sqlorder = pai_torder
end function
function qwbm_sqlorder(p_xuid,xml1)
pai_torder = ""
if qdatatype="sqlserver" then
pai_torder = " order by charindex(','+rtrim(cast(id as varchar(10)))+',',',"& p_xuid &",')"
else
pai_torder = " order by instr(',"& p_xuid &",',','&id&',')"
end if
qwbm_sqlorder = pai_torder
end function