select * from ttt t 
where exists(seelct 1 from ttt where name=t.name and addr<>t.addr)

解决方案 »

  1.   

    --创建测试环境
    create table ttt
    (
      name varchar(20),
      addr varchar(20)
    )
    insert ttt
    select '张三','中国北京市' union all
    select '李四','中国上海市' union all
    select '王五','中国天津市' union all
    select '张三','中国四川省'--测试
    select * from ttt t 
    where exists(select 1 from ttt where name=t.name and addr<>t.addr)
    --删除测试环境
    drop table ttt--结果
    /*
    name                 addr                 
    -------------------- -------------------- 
    张三                   中国北京市
    张三                   中国四川省(2 row(s) affected)
    */
      

  2.   

    --或者:
    select t.* from ttt t 
    join ttt A on t.name=A.name and t.addr<>A.addr
      

  3.   

    vivianfdlpw, 你这么快就上星了.
    PFPF啊.
      

  4.   

    vivian反应真快addr 可以相同的呀. 还能用这方法吗?
      

  5.   

    --创建测试环境
    create table ttt
    (
      name varchar(20),
      addr varchar(20)
    )
    insert ttt
    select '张三','中国北京市' union all
    select '李四','中国上海市' union all
    select '王五','中国天津市' union all
    select '张三','中国四川省'--测试
    declare @tb table(ID int identity,name varchar(20),addr varchar(20))
    insert @tb(name,addr) select * from ttt
    select name,addr from @tb t 
    where exists(select 1 from @tb where name=t.name and ID<>t.ID)
    --删除测试环境
    drop table ttt--结果
    /*
    name                 addr                 
    -------------------- -------------------- 
    张三                   中国北京市
    张三                   中国四川省(2 row(s) affected)
    */
      

  6.   

    to filebat(Mark):
    昨天就升星了:)
      

  7.   

    试了一下,如果去掉 and t.addr<>A.addr 就不称哦
      

  8.   

    表数据很大,建临时表 不妥吧.而且 identity字段只能用于 sql server.我现在只有oracle的环境.求一个 可移植的,通用的方法.
      

  9.   

    --创建测试环境
    create table ttt
    (
      name varchar(20),
      addr varchar(20)
    )
    insert ttt
    select '张三','中国北京市' union all
    select '李四','中国上海市' union all
    select '王五','中国天津市' union all
    select '张三','中国北京市' union all
    select '李四','中国深圳市'--测试
    select * from ttt t 
    where 
         (select count(1) from ttt where name=t.name)>1
    order by name--删除测试环境
    drop table ttt--结果
    /*
    name                 addr                 
    -------------------- -------------------- 
    李四                   中国上海市
    李四                   中国深圳市
    张三                   中国北京市
    张三                   中国北京市(4 row(s) affected)
    */
      

  10.   

    表 ttt( name varchar(20), addr varchar(20))其中 name有重复数据. 
    select * from ttt where name in (select name from ttt group by  name having count(name)>1)
      

  11.   

    filebat(Mark)比较后知后觉,我天天上,昨天就发现 vivianfdlpw() 后面的东西好像少了,呵呵
     原来他升星了,可惜不熟,否则一定问候一句,呵呵 问题都解决了,我也发一个,不过没有没有那么简便,惭愧:
    select * from ttt t 
     where exists  (select top b.name  from (select top 2 t1.name from ttt t1 
    where name=t1.name order by t1.name) b
    where b.name = t.name)
    order by name
      

  12.   

    select * from ttt t 
    where 
         (select count(1) from ttt where name=t.name)>1
    order by name
    好办法,呵呵学习!