CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

如何选择存在交集的两个表,请高手指教!!!!!!!!!!

楼主yuekai(BTS)2005-02-04 18:16:36 在 MS-SQL Server / 基础类 提问

如下两个表:  
  table1:  
  c1     c2  
  1     a,b,c,d  
  2     r,e,g,j,d    
  3     a,b  
  4     a  
  5     a,y  
  table2:  
  c3     c4  
  1       b,y  
  选出所有上面两个表存在交集的数据的SQL要如何写  
  得到的结果应该是  
  从table1.c1中选取记录为  
  1  
  3  
  5  
  由于1,3,5中不是包含b就是包含y  
  请高手指教!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 问题点数:80、回复次数:3Top

1 楼didoleo(冷月无声)回复于 2005-02-04 19:00:59 得分 40

create   table   tb1  
  (c1   int,c2   varchar(1000))  
   
  insert   into   tb1  
  select   1   ,   'a,b,c,d'           union   all  
  select   2   ,   'r,e,g,j,d'       union   all  
  select   3   ,   'a,b'     union   all  
  select   4   ,   'a'     union   all  
  select   5   ,   'a,y'  
   
   
  create   table   tb2  
  (c3   int,c4   varchar(1000))  
   
  insert   into   tb2  
  select   1   ,     'b,y'  
   
   
  --插分处理  
   
  --处理临时表  
  drop   table   #t2  
   
  declare   @i   int  
  declare   @j   int  
  select   @i=max(len(c2))   from   tb1    
  select   @j=max(len(c4))   from   tb2    
  set   rowcount   @i  
  select   id=identity(int)   into   #t1   from   syscolumns   a,syscolumns   b  
  set   rowcount   @j  
  select   id=identity(int)   into   #t2   from   syscolumns   a,syscolumns   b  
  set   rowcount   0  
   
   
  select   *   from   (  
  select   a.c1,  
  c2=substring(a.c2,b.id,charindex(',',a.c2+',',b.id)-b.id)  
  from   tb1   a,#t1   b  
  where   substring(','+a.c2,b.id,1)=','  
  )   a   where  
  exists       (  
  --order   by   a.c1,b.id  
  select   1   from   (  
  select    
  c4=substring(a.c4,b.id,charindex(',',a.c4+',',b.id)-b.id)  
  from   tb2   a,#t2   b  
  where   substring(','+a.c4,b.id,1)=','  
  )   b   where   a.c2=b.c4  
  )  
   
  --------------------------------  
  1 b  
  3 b  
  5 y  
   
   
  (所影响的行数为   3   行)Top

2 楼didoleo(冷月无声)回复于 2005-02-04 19:04:32 得分 40

--改一点,当中的drop   table   #t2不要  
  create   table   tb1  
  (c1   int,c2   varchar(1000))  
   
  insert   into   tb1  
  select   1   ,   'a,b,c,d'           union   all  
  select   2   ,   'r,e,g,j,d'       union   all  
  select   3   ,   'a,b'     union   all  
  select   4   ,   'a'     union   all  
  select   5   ,   'a,y'  
   
   
  create   table   tb2  
  (c3   int,c4   varchar(1000))  
   
  insert   into   tb2  
  select   1   ,     'b,y'  
   
   
   
   
  declare   @i   int  
  declare   @j   int  
  select   @i=max(len(c2))   from   tb1    
  select   @j=max(len(c4))   from   tb2    
  set   rowcount   @i  
  select   id=identity(int)   into   #t1   from   syscolumns   a,syscolumns   b  
  set   rowcount   @j  
  select   id=identity(int)   into   #t2   from   syscolumns   a,syscolumns   b  
  set   rowcount   0  
   
   
  select   *   from   tb1   where   c1   in   (  
  select   a.c1   from   (  
  select   a.c1,  
  c2=substring(a.c2,b.id,charindex(',',a.c2+',',b.id)-b.id)  
  from   tb1   a,#t1   b  
  where   substring(','+a.c2,b.id,1)=','  
  )   a   where  
  exists       (  
  select   1   from   (  
  select    
  c4=substring(a.c4,b.id,charindex(',',a.c4+',',b.id)-b.id)  
  from   tb2   a,#t2   b  
  where   substring(','+a.c4,b.id,1)=','  
  )   b   where   a.c2=b.c4  
  )  
  )  
  --------------------------------  
  c1               c2  
  1 a,b,c,d  
  3 a,b  
  5 a,y  
   
   
  (所影响的行数为   3   行)Top

3 楼yuekai(BTS)回复于 2005-02-04 19:38:33 得分 0

感谢,先看看,马上结贴Top

相关问题

  • 两个表如何交集再并集?
  • SQL语句中,UNION表示并集,如何表示交集和差集呢?
  • 悲喜交集
  • 我想做两个数据表的交集,请帮忙看看.等待中。
  • 两个结构相同的表,如何取得它们的数据交集?
  • 用单链表结构求两集合的交集出现问题
  • 要对两个表中 有交集的记录都删除。应该如何处理,谢谢!
  • mysql中,如何取交集??
  • vector求交集的问题
  • powerDesigner中表删除了,这个表还有一个外键,删除的时候选择了只删除表,又重建了一个和原来的表名一样的表,错误了,说是表已经存在

关键词

  • 表
  • tb
  • syscolumns
  • union allselect
  • rowcount
  • substring
  • table
  • select

得分解答快速导航

  • 帖主:yuekai
  • didoleo
  • didoleo

相关链接

  • SQL Server类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo