sql中带in条件的查询及提⾼效率ALTER PROCEDURE[dbo] . [example1]
(
@booker varchar ( 100 )
)
AS
declare@str varchar ( 1000 )
set@str='select * from tb_itregister where booker in('+@booker+')'
execute ( @str )
调⽤:
string booker = "a,b";
booker = booker. Replace (",", " ',' ");
booker = " '" + booker + "' ";
/************************************************************
问题描述: id值可能有数千个之多,怎么提⾼效率?
例⼦:select * from table1 where id in (1,3,6,10,...,8000) and type = 1
************************************************************/
--例如:
SELECT*
FROM tb
WHERE id IN (1, 2, 3, 4, ........)
AND NAME ='best'
join和in哪个查询更快--1.将括号的条件做成变量
DECLARE@str VARCHAR(4000)
SET@str='1,2,3,'
--2.然后将@s拆分后插⼊临时表
CREATE TABLE #t
(
id VARCHAR(10)
)
DECLARE@i INT
DECLARE@len INT
SET@i=1
WHILE@i<LEN(@str+',')
BEGIN
INSERT #t
SELECT SUBSTRING(@str+',', @i, CHARINDEX(',', @str+',', @i) -@i)
SET@i=CHARINDEX(',', @str+',', @i) +1
END
--3利⽤临时表和原表进⾏连接取值
SELECT k.*
FROM tb k
INNER JOIN #t p
ON p.id = k.id
WHERE NAME ='best'
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论