日期:2014-05-18  浏览次数:20512 次

一个关于查询的语句
数据库里面有2张 结构完全一样的表 【A】 【B】


A 表结构: 1 | 2 | 3 | 甲 | 乙 | 丙 | a | b | c | NO.

  张三| 安徽 | 合肥 |130123456| 移动| 100元 | 淘宝| 店铺| 憨憨选乐| 1

  李四| 上海 | 浦东 |130654321| 移动| 200元 | 淘宝| 店铺| 憨憨选乐| 2
  王武| 青岛 | 随便 |131123456| 移动| 50元 | 淘宝| 店铺| 憨憨选乐| 3



 




B 表结构: 1 | 2 | 3 | 甲 | 乙 | 丙 | a | b | c | NO.
  张三| 安徽 | 合肥 |130123456| 移动| 100元 | 淘宝| 店铺| 憨憨选乐| 1
  李四| 上海 | 浦东 |130654321| 移动| 200元 | 淘宝| 店铺| 憨憨选乐| 2
  王武| 福建 | 泉州 |131123456| 移动| 50元 | 淘宝| 店铺| 憨憨选乐| 3

现在要在 B表中 找出 A表 和 B表 不想同的 数据 NO.是关键字 2个表相同项目的NO.都一样 现在 王武这项 A B表的 2 3内容不一样

当 1 2 3 中间 只要有一个不相同 则输出 只包含 1 2 3 NO. 这 4 项 的记录 其他的 用空格 代替 

当 甲 乙 丙 中间 只要有一个不相同则 输出 只包含 1 2 3 NO. 甲 乙 丙 这 7 项 的记录 其他的 用空格 代替 

当 a b c 中间 只要有一个不相同则 输出 只包含 1 2 3 a b c NO. 这 7 项 的记录 其他的 用空格 代替 

实际上的 意思 就是 1 2 3 NO. 必须 输出 然后 甲乙丙 为 一组 a b c 为一组 每 一组 只要有一个属性不同则输出该组数据 


不知道 我讲清楚没有 跪求 代码 谢谢了 ·· 做个网页 搞了一下午 控制不来 表格 只能 用数据库语句了 

   

 

------解决方案--------------------
用case when 可以解决

SQL code

select 
(case when t1.1=t2.1 and t1.2=t2.2 and t1.3=t2.3 then ' ' else t1.1 end) a1, 
(case when t1.1=t2.1 and t1.2=t2.2 and t1.3=t2.3 then ' ' else t1.2 end) a2,
(case when t1.1=t2.1 and t1.2=t2.2 and t1.3=t2.3 then ' ' else t1.3 end) a3,
(case when t1.甲=t2.甲 and t1.乙=t2.乙 and t1.丙=t2.丙 then ' ' else t1.甲 end) a甲,   
(case when t1.甲=t2.甲 and t1.乙=t2.乙 and t1.丙=t2.丙 then ' ' else t1.乙 end) a乙, 
(case when t1.甲=t2.甲 and t1.乙=t2.乙 and t1.丙=t2.丙 then ' ' else t1.丙 end) a丙,
(case when t1.a=t2.a and t1.b=t2.b and t1.c=t2.c then ' ' else t1.a end) aa,
(case when t1.a=t2.a and t1.b=t2.b and t1.c=t2.c then ' ' else t1.b end) ab,
(case when t1.a=t2.a and t1.b=t2.b and t1.c=t2.c then ' ' else t1.c end) ac,
NO,
(case when t1.1=t2.1 and t1.2=t2.2 and t1.3=t2.3 then ' ' else t2.1 end) b1, 
(case when t1.1=t2.1 and t1.2=t2.2 and t1.3=t2.3 then ' ' else t2.2 end) b2,
(case when t1.1=t2.1 and t1.2=t2.2 and t1.3=t2.3 then ' ' else t2.3 end) b3,
(case when t1.甲=t2.甲 and t1.乙=t2.乙 and t1.丙=t2.丙 then ' ' else t2.甲 end) b甲,   
(case when t1.甲=t2.甲 and t1.乙=t2.乙 and t1.丙=t2.丙 then ' ' else t2.乙 end) b乙, 
(case when t1.甲=t2.甲 and t1.乙=t2.乙 and t1.丙=t2.丙 then ' ' else t2.丙 end) b丙,
(case when t1.a=t2.a and t1.b=t2.b and t1.c=t2.c then ' ' else t2.a end) ba,
(case when t1.a=t2.a and t1.b=t2.b and t1.c=t2.c then ' ' else t2.b end) bb,
(case when t1.a=t2.a and t1.b=t2.b and t1.c=t2.c then ' ' else t2.c end) bc
from a as t1 inner join b as t2 on t2.no=t1.no

------解决方案--------------------
select isnull(aa.no,isnull(bb.no,cc.no)) no,col1,col2,col3,甲,乙 ,丙,a,b,c from 
(select a.no,b.col1,b.col2,b.col3 from a join b on a.no=b.no and a.col1+a.col2+a.col3<>b.col1+b.col2+b.col3) aa full outer join 
(select A.甲 , A.乙 , A.丙,a.no from a join b on a.no=b.no and A.甲 + A.乙 + A.丙<>b.甲 + b.乙 + b.丙) bb on aa.no=bb.no full outer join 
(select A.a , A.b , a.c,a.no from a join b on a.no=b.no and A.a + A.b + A.c<>b.a + b.b + b.c) cc on bb.no=cc.no

------解决方案--------------------
SQL code

select isnull(aa.no,isnull(bb.no,cc.no)) no,col1,col2,col3,甲,乙 ,丙,a,b,c from  
(select a.no,b.col1,b.col2,b.col3 from a join b on a.no=b.no and a.col1+a.col2+a.col3<>b.col1+b.col2+b.col3) aa full outer join  
(select A.甲 , A.乙 , A.丙,a.no from a join b on a.no=b.no and A.甲 + A.乙 + A.丙<>b.甲 + b.乙 + b.丙) bb on aa.no=bb.no full outer join  
(select A.a , A.b , a.c,a.no from a join b on a.no=b.no and A.a + A.b + A.c&l