select * from tb t where exists(select 1 from tb where col2=t.col2 and col1<>t.col1)

解决方案 »

  1.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2014-03-29 12:48:34
    -- Verstion:
    --      Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
    -- Jul  9 2008 14:43:34 
    -- Copyright (c) 1988-2008 Microsoft Corporation
    -- Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([COL1] varchar(1),[COL2] int)
    insert [tb]
    select 'A',11 union all
    select 'B',11 union all
    select 'C',12 union all
    select 'D',13
    --------------开始查询--------------------------
    select * from tb t where exists(select 1 from tb where col2=t.col2 and col1<>t.col1)
    ----------------结果----------------------------
    /* COL1 COL2
    ---- -----------
    A    11
    B    11(2 行受影响)*/
      

  2.   


    SELECT * FROM tb  abc WHERE (SELECT COUNT(*)FROM tb WHERE tb.COL2=abc.col2)>1
      

  3.   


    SELECT a.*
    FROM 表 a
    JOIN 
    (
    SELECT COL2
    FROM 表
    GROUP BY COL2
    HAVING COUNT(*)>1
    ) b ON a.COL2=b.COL2