当前位置: 代码迷 >> Sql Server >> *查询相同字段的记要*
  详细解决方案

*查询相同字段的记要*

热度:78   发布时间:2016-04-24 09:24:16.0
****查询相同字段的记录*****
表A
id                                                                  name
00000004,00000005,00000008 单杠,双杠,EE
00000006,00000007                              CC,DD


表B
jobno                                            id           name
OU2015012800001 00000008 AA
OU2015012800001         00000009      BB
OU2015012800002         00000004 单杠
OU2015012800002 00000005 双杠
OU2015012800002         00000008 EE
OU2015012800003 00000007 DD


得到jobno--OU2015012800002

想从表B中找到ID号完全等于表A中ID的JOBNO号

表A记录一组ID及对应的说明,从表B中找到ID号完全等于表A中ID的JOBNO号
------解决思路----------------------
with a(id,name) as
(select '00000004,00000005,00000008','单杠,双杠,EE'
 union all
 select '00000006,00000007','CC,DD')
 ,b(jobno,id,name) as
 (select 'OU2015012800001','00000008','AA' union all
  select 'OU2015012800001','00000009','BB' union all
  select 'OU2015012800002','00000004','单杠' union all
  select 'OU2015012800002','00000005','双杠' union all
  select 'OU2015012800002','00000008','EE' union all
  select 'OU2015012800003','00000007','DD')
  
  select jobno  from 
  (select aid,jobno,COUNT(*) as 个数   from 
  (select a.id as aid,b.jobno,b.id as bid from a,b
  where CHARINDEX(','+b.id+',',','+a.id+',')<>0
  group by a.id,b.jobno,b.id) as c group  by
  c.aid,c.jobno) as d  inner join 
  (select *,LEN(id)-LEN(replace(id,',',''))+1 as 个数
  from a) as e  on d.aid=e.id and d.个数=e.个数
  
  
  /*
      jobno
  ---------------
  OU2015012800002
  ----------------
  */
  再试试
  相关解决方案