当前位置:网站首页>mysql查询表中重复记录
mysql查询表中重复记录
2022-08-02 21:51:00 【模糊化的】
mysql查询表中某个字段值重复的记录,
也就是说,该字段值出现的个数是>=2,所以可以对该字段进行分组,然后对字段count计数,再筛选出>=2的count的值即可,具体实现如下:
select * from tb where 字段 in(
select 字段 from tb group by 字段 having count(字段)>=2);
查询多个字段值重复的记录:和上面类似,只不过分组和查询条件变成了多个字段:
select * from tb where (datetime,user) in
(select datetime,user from tb group by datetime,user having count(*)>=2);
过滤掉重复字段(完全重复的记录):
select distinct * from tb;
过滤掉重复字段(某些重复的字段值)
select * from tb where id in(select max(id) from tb group by 字段1,字段2)
删除有重复值的记录(针对单个字段的重复值),只保留id最大的那个:
delete from tb_test where id not in (select id from
(select max(id) id from tb_test group by datetime) t);
删除有重复值的记录(多个字段的重复值),只保留id最大的那个:
在group by 后面放入多个字段,逗号隔开
值得注意的是,要将查询到的Max值存入一个临时表,否则报错:You can’t specify target table for update in FROM clause
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- Broadcast platform, the use of the node generated captcha image, and validate
- Teach you how to kill if else
- 宝塔搭建实测-基于ThinkPHP5.1的wms进销存源码
- IP Protocol (Internet Protocol)
- Add and delete all these years, finally planted in MySQL architecture design!
- Jmeter二次开发实现rsa加密
- Tanabata is here - the romance of programmers
- CS5213 chip | HDMI to VGA converter chip data sharing
- word操作:单独调整英文字体
- 创建型模式 - 简单工厂模式StaticFactoryMethod
猜你喜欢

如何抓住NFT、元|宇|宙新趋势?

目前为止 DAO靠什么盈利?

Flink优化的方方面面

Win10怎么开启自带的游戏录屏功能?

Word operation: adjust the English font individually

CKA、CKAD、CKS、KCNA、CFCD考试

Jmeter二次开发实现rsa加密

牛客每日刷题之链表

What is the core business model of the "advertising e-commerce" that has recently become popular in the circle of friends, and is the advertising revenue really reliable?

Software testing pen questions 1 (with answers)
随机推荐
Ansible安装与配置
面试官:可以谈谈乐观锁和悲观锁吗
Summary of @Transactional transaction invocation and effective scenarios
Intensive reading of the Swin Transformer paper and analysis of its model structure
如何通过开源数据库管理工具 DBeaver 连接 TDengine
Ruoyi integrates minio to realize distributed file storage
不堪哥哥殴打谩骂,妹妹申请人身安全保护令获支持
你离「TDengine 开发者大会」只差一条 SQL 语句!
Flink-shell
AcWing 2983. 玩具
@Transactional 事务调用与生效场景总结
【TypeScript】深入学习TypeScript模块化
js function anti-shake and function throttling and other usage scenarios
IP协议(网际协议)
JumpServer open source bastion machine completes Loongson architecture compatibility certification
CodeTON Round 2 A - D
How does Redis easily achieve system instant kill?
go 序列化与反序列化
目前为止 DAO靠什么盈利?
双轴晶体中锥形折射的建模与应用