参数:@p0=1,2,3,4

​1.我们普通的查询如下:​

​select​​ ​​*​​​​from​​ ​​table_name t ​​​​where​​ ​​t.field1​​​​in​​ ​​(1,2,3,4,...);​


​如果需要传参数的话​

​select *from table_name t where t.field1in (@p0);​

这样最终的sql是​​select​​ ​​*​​​​from​​ ​​table_name t ​​​​where​​ ​​t.field1​​​​in​​ ​​(‘1,2,3,4,...‘);​

​只能查出t.field1=1的结果(具体为什么能查出第一个记录待研究)​


​要想所有参数都能查出的话这样是不行的,这时候需要使用第二中解决方案​


​2.find_in_set 使用这个方法来搞定​

​select *from table_name t where  find_in_set(t.field1,'1,2,3,4');​

​下面是相关排序和sqlserver的解决方案​

 



MySQL/sqlserver查询in操作 查询结果按in集合顺序显示


select * from ibs6_terminal_adv_inf where id in (16,14,15) order by field(id,16,14,15)

select * from ibs6_terminal_adv_inf where id in (16,14,15) order by find_in_set(id,'16,14,15')

select * from ibs6_terminal_adv_inf where id in (16,14,15) order by substring_index('16,14,15',id,1)

结果就是按顺序显示了

sqlserver 用以下的语句

select * from ibs6_terminal_adv_inf where id in (16,14,15)  order by CHARINDEX(','+ltrim(id)+',',',16,14,15,')"


这个方法没有测试过,有测试过的同学可以发一下结果