表一
         A        B        C       D
-----------------------------------------
设备1    1        1        1       0
设备2    0        1        0       0
设备3    1        1        0       0
设备4    0        0        1       1
设备5    1        0        1       1
设备6    1        1        0       1
设备7    1        1        0       0
设备8    0        0        1       1表二
         A        B        C       D
-----------------------------------------
设备1    1        0        1       1
设备2    1        1        0       0
设备3    1        1        0       0
设备4    0        0        1       1
设备5    1        0        0       1
设备6    1        0        1       0
设备7    1        1        0       0
设备8    0        0        1       1问题是这样的,我需要监控多个设备的各项运行指标的变化情况,我用的这个方法,把某一时刻设备的各项运行
指标交替存入两个表(就是说这次存在表一,下次存在表二),两个表结构完全一样,数据定期刷新,现在需要
及时查询出两个表的数据不一致的地方。请问各位大哥怎么写一个高效的 SQL 语句?或者说有更好地的解决办法?谢谢!

解决方案 »

  1.   

    select
        a.设备,
        case when a.A=b.A then '不变' else '变' end,
        case when a.B=b.B then '不变' else '变' end,
        case when a.C=b.C then '不变' else '变' end,
        case when a.D=b.D then '不变' else '变' end
    from
        表一 a,
        表二 b
    where
        a.设备=b.设备
      

  2.   

    create table tb1(name varchar(20),A  int,       B  int,      C       int ,D int )
    -----------------------------------------
    insert into tb1  select '设备1' ,   1 ,       1    ,    1    ,   0 union select 
    '设备2' ,   0        ,1       , 0       ,0
    create table tb2(name varchar(20),A  int,       B  int,      C       int ,D int )-----------------------------------------
    insert into tb2  select '设备1',    1   ,     0 ,       1   ,    1 union select 
    '设备2'   , 1  ,      1   ,     0       ,0
    select *
    from tb1 a join tb2 b on a.name=b.name
    where a.a<>b.a or a.b<>b.b or a.c<>b.c or a.d<>b.d
      

  3.   

    libin_ftsafe(子陌红尘) 的写法最合适!
      

  4.   

    如果每次查看数据都如楼主所列出的数据,那么最高效的是:
    从表一中找出 与表二中记录不一致的记录: SQL语句为select * from 表一 where checksum(*) not in(select checksum(*) from 表二)
      

  5.   

    lsqkeke(可可) 我对数据定期刷新的做法确实是你的理解,你觉得有更好的方法么?