|
发表于 2020-5-1 10:00:01
|
显示全部楼层
create table a(aname varchar(100))
declare @str varchar(100),@T_sql varchar(8000)
set @str ='aaaa,bbbb,cccc,dddd'
set @T_sql='insert a select'''
set @T_sql=@T_sql+replace(@str,',',''' union all select ''')+''''
--select @T_sql
exec(@T_sql)
--select * from a
declare @t table(id int identity(1,1),keyword varchar(100))
insert @t select 'aaaa,bbbb,cccc,dddd'
union all select 'aaaa,bbbb'
union all select 'aaaa'
union all select 'aaaa,bbbb,cccc'
union all select 'eeee,ffff'
--select * from @t
--select keyword,a.* from a right join @t b on charindex(','+a.aname+',',','+b.keyword+',')>0
select keyword from a inner join @t b on charindex(','+a.aname+',',','+b.keyword+',')>0 group by keyword order by count(*) desc
drop table a
昨天为了你的问题我都加了半个小时班了,当时考虑得有点多了,想把查询条件关联表中不存在的数据也检索出来,比如a,b,c,d;后来在回家的路上才想起是白考虑了!哎,浪费了半个小时 |
|