多選項查詢,適用於愛好等的搜尋,有時何必行轉列

2021-06-08 09:03:02 字數 1937 閱讀 1159

create table #t(

ids int identity,

name varchar(100)

)insert into #t(name)

select '1,2,3'

union all

select '4,2,3'

union all

select '5,2,3'

union all

select '6,2,3'

union all

select '1,4,3'

union all

select '1,5,3'

union all

select '1,6,3'

union all

select '1,2,4'

union all

select '1,2,5'

union all

select '1,2,6'

union all

select '7,8,9'

union all

select '1,8,9'

union all

select '2,8,9'

union all

select '3,8,9'

union all

select '4,8,9'

union all

select '5,8,9'

union all

select '6,8,9'

union all

select '5,6,9'

union all

select '5,7,9'

union all

select '5,3,9'

union all

select '5,0,9'

select * from #t where exists(

select * from dbo.split(#t.name,',') as n

inner join (select * from dbo.split('5,9',',')) as m

on n.f1=m.f1)結果

同理:select * from #t where exists(

select * from dbo.split(#t.name,',')

where f1 in (5,9)

)你看懂了嗎?

付:split 函式

alter function [dbo].[split]

(@sourcesql varchar(8000),

@strseprate varchar(10)

)returns @temp table(f1 varchar(100))

asbegin

declare @i int

set @sourcesql = rtrim(ltrim(@sourcesql))

set @i = charindex(@strseprate,@sourcesql)

while @i >= 1

begin

if len(left(@sourcesql,@i-1))>0

begin

insert @temp values(left(@sourcesql,@i-1))

endset @sourcesql=substring(@sourcesql,@i+1,len(@sourcesql)-@i)

set @i=charindex(@strseprate,@sourcesql)

endif @sourcesql <> ''

insert @temp values(@sourcesql)

return

end

折半查詢 適用於順序陣列

折半查詢的思路 在順序陣列中,找到初始值 最小值 中間值,最大值,分別以low,mid,high表示。然後進行迴圈查詢,例如圖 low 1,high 16,mid low high 2 如果key值剛好等於中間值mid,則返回中間值。如果所求值key比中間值大,則在mid與high之間在進行第二次查...

適用於python的 vimrc檔案

根據我的需求做了一些小的改動。file vimrc date 2009 09 22 author gashero note 配置乙份簡單的vim配置檔案 set nocompatible 非相容模式 syntax on 開啟語法高亮 set background dark 背景色 color des...

折半查詢法(僅適用於已排好順序)

includeusing namespace std templateint binarysearch type array,int lenght,type key 不適用於char 型別 array mid key right mid 1 left mid 1 return 1 int main ...