select top 6 id,title from [mytable] where id in (34,279,34,44,45,33,46,279,45,46) order by charindex(','+id'+',', ','+'34,279,34,44,45,33,46,279,45,46'+',')
select top 6 id,title from [mytable] where id in (34,279,34,44,45,33,46,279,45,46), IN()裡面重複什麼意思?
手误select top 6 id,title from [mytable] where id in (34,279,34,44,45,33,46,279,45,46) order by charindex(','+id+',', ','+'34,279,34,44,45,33,46,279,45,46'+',')
order by charindex(','+id'+',', ','+'34,279,34,44,45,33,46,279,45,46'+',')
改好了,谢各位朋友,实现的确实很巧妙。order by charindex(','+cast(id as nvarchar(10))+',', ','+'34,279,34,44,45,33,46,279,45,46'+',')
order by charindex(','+cast(id as nvarchar(10))+',', ','+'34,279,34,44,45,33,46,279,45,46'+',') 对于这部分有查询效率上的当心,如果条件里id比较多,会不会存在性能问题?如不这样加排序的话,多少都不会担心查询性能,因为分析他是“聚集索引查找”但加了排序后,竟然还是“聚集索引查找”,有点不解
IN()裡面重複什麼意思?
order by charindex(','+id+',', ','+'34,279,34,44,45,33,46,279,45,46'+',')
对于这部分有查询效率上的当心,如果条件里id比较多,会不会存在性能问题?如不这样加排序的话,多少都不会担心查询性能,因为分析他是“聚集索引查找”但加了排序后,竟然还是“聚集索引查找”,有点不解
因为是先按照条件找,然后是排序,最后是取TOP
如果你的结果数据量很大,就存在性能了。