sql语句in 后面跟的数据过多怎么解决

2025-03-27 09:48:55
推荐回答(2个)
回答1:

解决办法,使用临时表:

第一步:创建临时表,并将in内的数据插入到表中

select * into # from (
select '1' num union all
select '2' union all
select '3' union all
.....
select 'N'
) a

第二步:执行in查询

select * from 表 where 列 in(select num from #)

第三步:销毁临时表

drop table #


若是连起来写:

set nocount on;select * into # from (select '1' num union all select '2' union all select '3' union all ..... select 'N') a;select * from 表 where 列 in(select num from #);drop table #

回答2:

那就使用 not in 后面的集合不就会减少