有A,B2张表,A表有个主键ID
B表有 ID,DATA1,DATA2,DATA3,FLAG字段,其中ID关联A表,FLAG有0,1两种,0表示自动获取,1表示用户输入,
B表总相同ID可能有多条记录,比如ID为3的记录有6条,其中flag为0的有3条,flag为1的有3条(也可以是2条或其他),现在要求比对所有的记录,找出用户输入和获取不一致的记录(必须是完全相同)的ID

解决方案 »

  1.   


    select a.ID from B a where a.ID in (select b.ID from B b and a.FLAG <> b.FLAG)
    或者
    select a.ID from B a, B b where a.ID = b.ID and a.FLAG <> b.FLAG
     
      

  2.   

    你没有判断他数据是不是一样啊,data1,data2,data3都是数据,必须判断用户输入的与系统获取的完全一致
      

  3.   

    感觉你的A表好像用不到,自己写了个,和楼上的一个意思。。
    select b.id
    from  b b1,b b2
    where b1.id=b2.id and b1.flag<>b2.flag
      

  4.   

    id data1 data2 data3 flag
     1   1     1     1     1
     2   2     2     2     1
     1   2     1     1     0
     2   2     2     2     0
     2   2     1     1     0
     3   3     3     3     1
     3   3     3     3     0
    id为1的是我要找出来的类型,应该flag=0和flag=1的时候 data1不一样
    id为2时也是,因为系统得到了2条记录,而用户只输入一条
    id为3的情况才是正确的
      

  5.   

    下次把问题说清楚,直接给个例子多好呀
    declare @tb table(id int, data1 int, data2 int, data3 int, flag int)
    insert @tb
    select  1,   1,     1,     1,     1 
    union all select 2,   2,     2,     2,     1 
    union all select 1,   2 ,    1,     1,     0 
    union all select 2,   2,     2,     2,     0 
    union all select 2,   2,     1,     1,     0 
    union all select 3,   3,     3,     3,     1 
    union all select 3,   3,     3,     3,     0 select distinct a.ID from @tb a where a.ID in 
    (select b.ID from @tb b 
    where a.ID = b.ID and a.FLAG <> b.FLAG 
    and 
    (a.data1<>b.data1 or a.data2<>b.data2 or a.data3<>b.data3))
    /*
    ID
    -----------
    1
    2(2 row(s) affected)
    */
      

  6.   

    哦,想起来了,还有个条件,如果只有系统获取没有用户输入的(即FLAG=0)的不算
    即类似下面这条不算
    4   4   4   4  0
      

  7.   

    想到了,group by id having sum(flag) >0