test_id       test_context     
1                        A
2                        B
5                        O
3                        J
5                        K
3                        C
5                        O
如上方,是一张表,我要怎么写查询语句选出test_id有重复的记录且test_context不同的记录要保留,剔除test_id无重复和test_id重复且test_context相同的记录。查询结果如:
test_id          test_context
3                          C
3                          J
5                          K
5                          O
查询语句怎么写呢?表的记录有35多万条,考虑高效。有大神能帮忙吗?重赏

解决方案 »

  1.   


    select distinct *  from tb A where exists(select 1 from tb B where A.id=B.id and A.content<>B.content)
      

  2.   


    SELECT distinct * from ttk a where exists(select 1 from ttk where a.test_id=test_id and a.test_context<>test_context  )
      

  3.   

     select distinct t1.* from t1,(select test_id from t1 group by test_id having count(distinct test_context)>1) t2 where t1.test_id=t2.test_id order by test_id;
      

  4.   

    select *
    from 是一张表
    group by test_id,test_context