当前位置: 代码迷 >> Sql Server >> 请问怎样查询 两个字段中用逗号分隔的字符串是相等的(分隔顺序不一样)
  详细解决方案

请问怎样查询 两个字段中用逗号分隔的字符串是相等的(分隔顺序不一样)

热度:67   发布时间:2016-04-24 10:30:32.0
请教怎样查询 两个字段中用逗号分隔的字符串是相等的(分隔顺序不一样)
请教怎样查询 两个字段中用逗号分隔的字符串是相等的(分隔顺序不一样)

一个字段的内容是:100,200,300,400
另一个字段的内容如果是:400,300,100,200 (或是 300,400,200,100 )不管顺序如何,都能够查询出来
但是如果字段的内容是:100,200,300   只要少一项就不要查询出来
意思就是说能够查询分隔符分隔的顺序不同但是内容相同的记录,不知道我说的明白不明白,请教高手!!谢谢拉!

------解决方案--------------------
转成行的方式再用in来查找
if OBJECT_ID('test') is not null
drop table test 
go 
create table test 

    id int, 
    name varchar(10), 
    [key] varchar(20) 

go 
insert test 
select 1,'lisa','li,is,sa' union all
select 2,'sophia','ab,cd,ef' union all
select 3,'lori','12,34,23'
go 
  
select
    id, 
    a.name, 
    SUBSTRING([key],number,CHARINDEX(',',[key]+',',number)-number) as [key] 
from
    test a,master..spt_values 
where
    number >=1 and number<=len([key])  
    and type='p' 
    and substring(','+[key],number,1)=','
/* 
id    name    key
----------------------------- 
1    lisa    li 
1    lisa    is
1    lisa    sa 
2    sophia    ab 
2    sophia    cd 
2    sophia    ef 
3    lori    12 
3    lori    34 
3    lori    23 
*/ 

------解决方案--------------------
思路:写一个函数传入2个需要比较的字段,然后通过间隔符转换为数据集进行比较,通过间隔符转换为数据集的方法:
CREATE function [dbo].[fn_split](@SourceSql VARCHAR(MAX),@StrSeprate varchar(10))
--@SourceSql : 需要拆分字符串
--@StrSeprate:分隔符
returns @temp table(AllItem varchar(100))
--实现split功能 的函数
as 
begin
declare @i int
set @SourceSql=rtrim(ltrim(@SourceSql))
set @i=charindex(@StrSeprate,@SourceSql)
while @i>=1
begin
insert @temp values(left(@SourceSql,@i-1))
set @SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i)
set @i=charindex(@StrSeprate,@SourceSql)
end
if @SourceSql<>'\'
insert @temp values(@SourceSql)
return 
end

------解决方案--------------------
先写一个函数,给这个字段用逗号分隔的数据进行排序来转换成升序的数据,然后再写这个SQL语句

SELECT * FROM dbo.fn_convert(a)=dbo.fn_convert(b) and a<>b
这种排序的函数我想楼主肯定会写,这是程序员基础,这种排序的方法太多了,写起来太麻烦了,我就不写了,如果不能给分我也就认了,给个思路吧。
------解决方案--------------------
引用:
先写一个函数,给这个字段用逗号分隔的数据进行排序来转换成升序的数据,然后再写这个SQL语句

SELECT * FROM dbo.fn_convert(a)=dbo.fn_convert(b) and a<>b
这种排序的函数我想楼主肯定会写,这是程序员基础,这种排序的方法太多了,写起来太麻烦了,我就不写了,如果不能给分我也就认了,给个思路吧。
create table #tmp([A] int primary key, [B] nvarchar(500))
insert into #tmp
select 1,'100,200,300,400' union all
select 2,'100,300,200' union all
select 3,'400,300,100,200' union all
select 4,'400,200,100,500' union all
select 5,'400,200,100,300';
go
create function f_strsort(@s varchar(500))
returns varchar(5001) as
begin
  declare @r varchar(500);
  set @r='';
  with t1 as
  (
    select SUBSTRING(@s,number,CHARINDEX(',',@s+',',number)-number) t
    from master..spt_values
    where number >=1 and number<=len(@s)  
      and type='p' 
      and substring(','+@s,number,1)=','
  ),
  t2 as
  (
    select ROW_NUMBER() over(order by t) id, t
    from t1
  )
  select @r=@r+','+t from t2 order by id
  return stuff(@r,1,1,'')
end
go

select * from #tmp
where dbo.f_strsort(B)=dbo.f_strsort('100,300,400,200')

drop table #tmp
drop function f_strsort
/*--------结果---------
A B
1 100,200,300,400
3 400,300,100,200
5 400,200,100,300
*/

------解决方案--------------------
再练 xml path
create table #tmp([A] int primary key, [B] nvarchar(500))
insert into #tmp
select 1,'100,200,300,400' union all
select 2,'100,300,200' union all
select 3,'400,300,100,200' union all
  相关解决方案