日期:2014-05-18 浏览次数:20475 次
if object_id('[wb]') is not null drop table [wb] go create table [wb]([ID] varchar(1),[QuestionClassification] varchar(27)) insert [wb] select 'a','1,2,3,4,5,6,7' union all select 'b','11,22,33,44,55,66,77' union all select 'c','111,222,333,444,555,666,777' go if object_id('[wu]') is not null drop table [wu] go create table [wu]([id] int,[name] varchar(2),[KeyWord] varchar(8)) insert [wu] select 1,'aa','1,22,333' union all select 2,'bb','111,1,22' union all select 3,'cc','22,33' go select a.* from wu a join ( select substring(QuestionClassification,b.number,charindex(',',QuestionClassification+',',b.number)-b.number) as num from wb a join master..spt_values b on b.type='P' and charindex(',',','+QuestionClassification,b.number)=b.number where ID='a' ) b on charindex(','+b.num+',',','+a.keyword+',')>0 /** id name KeyWord ----------- ---- -------- 1 aa 1,22,333 2 bb 111,1,22 (2 行受影响) **/
------解决方案--------------------
/* create function [dbo].[m_split](@c varchar(2000),@split varchar(2)) returns @t table(col varchar(200)) as begin while(charindex(@split,@c) <>0) begin insert @t(col) values (substring(@c,1,charindex(@split,@c)-1)) set @c = stuff(@c,1,charindex(@split,@c),'') end insert @t(col) values (@c) return end */ declare @wb table([ID] varchar(1),[QuestionClassification] varchar(27)) insert @wb select 'a','1,2,3,4,5,6,7' union all select 'b','11,22,33,44,55,66,77' union all select 'c','111,222,333,444,555,666,777' declare @wu table([id] int,[name] varchar(2),[KeyWord] varchar(8)) insert @wu select 1,'aa','1,22,333' union all select 2,'bb','111,1,22' union all select 3,'cc','22,33' ;with maco as ( select * from [dbo].[m_split]((select [QuestionClassification] from @wb where ID='a'),',') ) select * from @wu a ,maco b where charindex(','+ltrim(b.col)+',',','+a.[KeyWord]+',')>0 /* id name KeyWord col ----------- ---- -------- ------------------------------ 1 aa 1,22,333 1 2 bb 111,1,22 1 */
------解决方案--------------------