mysql 查询表中字段值相同的记录

xingfuxiangyi 2014-10-23 01:11:29
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多万条,考虑高效。有大神能帮忙吗?重赏
...全文
6936 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ACMAIN_CHM 2014-10-23
  • 打赏
  • 举报
回复
select * from 是一张表 group by test_id,test_context
CT_LXL 2014-10-23
  • 打赏
  • 举报
回复
 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;
wwwwb 2014-10-23
  • 打赏
  • 举报
回复
SELECT distinct * from ttk a where exists(select 1 from ttk where a.test_id=test_id and a.test_context<>test_context )
rucypli 2014-10-23
  • 打赏
  • 举报
回复
select distinct * from tb A where exists(select 1 from tb B where A.id=B.id and A.content<>B.content)

56,679

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧