stru_id,  stru_name,  location
10102     总部           上海
10102     总部1          上海
10102     总部           上海
10103     分部           北京
10103     分部           北京
10103     分部           北京
现在想要查出stru_id和stru_name不匹配的数据 
例如:当stru_id=10102时,是与stru_name不匹配的,因为存在stru_name为"总部1",其他都是"总部"
      当stru_id=10103时,是与stru_name匹配的请问各位大哥们
怎么弄? 
我很菜鸟~
麻烦讲的越详细越好!
我是真不会使才问的您们

解决方案 »

  1.   

    那location一定是一样的,还是不一样?
    如果不一样呢?是否不管这列?
      

  2.   

    declare @t table(stru_id int ,  stru_name nvarchar(10),  location nvarchar(10))
    insert @t select 10102 ,   N'总部'    ,      N'上海' 
    insert @t select 10102 ,   N'总部1'   ,       N'上海' 
    insert @t select 10102 ,   N'总部'    ,      N'上海' 
    insert @t select 10103 ,   N'分部'    ,      N'北京' 
    insert @t select 10103 ,   N'分部'    ,      N'北京' 
    insert @t select 10103 ,   N'分部'    ,      N'北京'
    select * from @t t where exists(select 1 from @t where stru_id=t.stru_id and stru_name<>t.stru_name)
    /*
    stru_id     stru_name  location   
    ----------- ---------- ---------- 
    10102       总部         上海
    10102       总部1        上海
    10102       总部         上海*/
      

  3.   

    declare @tb table(stru_id int,  stru_name varchar(20),  location varchar(20)) 
    insert @tb 
    select 10102,    '总部'  ,        '上海'  union all
    select 10102  ,  '总部1' ,         '上海' union all
    select 10102   , '总部'  ,        '上海' union all
    select 10103  ,  '分部'  ,        '北京' union all
    select 10103  ,  '分部'  ,       '北京' union all
    select 10103  ,  '分部'  ,        '北京' select b.* from(
    select stru_id from
    (select distinct * from @tb) a
    group by stru_id
    having count(stru_id)>1) c
    join @tb b on c.stru_id=b.stru_idstru_id     stru_name            location             
    ----------- -------------------- -------------------- 
    10102       总部                   上海
    10102       总部1                  上海
    10102       总部                   上海(所影响的行数为 3 行)