当前位置:网站首页>MYSQL去重方法汇总
MYSQL去重方法汇总
2022-04-23 04:06:00 【解忧杂货铺Q】
表数据
备注:
- id: 任务id;
- name: 参与人name;
1:distinct
1.1
-- 根据任务ID去重
SELECT DISTINCT id FROM test;
1.2
-- 任务总数
SELECT COUNT(DISTINCT id) FROM test;
1.3
distinct 通常效率较低。它不适合用来展示去重后具体的值,一般与 count 配合用来计算条数
。
distinct 使用中,放在 select 后边,对后面所有的字段的值统一进行去重。比如distinct后面有两个字段,那么 11,11 和 11, 21 这两条记录不是重复值
SELECT DISTINCT id, name FROM test;
2: group by
2.1
SELECT id, name, count(*) FROM test
GROUP BY id;
-- 任务总数
SELECT
count( tmp.id )
FROM
( SELECT id, NAME FROM test GROUP BY id ) tmp
3:row_number
row_number 是窗口函数,语法如下:
row_number() over (partition by <用于分组的字段名> order by <用于组内排序的字段名>) 其中partition by 部分可省略
SELECT
id,
name,
ROW_NUMBER() over (ORDER BY id) rn
FROM test
SELECT
id,
name,
ROW_NUMBER() over (PARTITION by id ORDER BY id) rn
FROM test
SELECT
COUNT( CASE WHEN rn = 1 THEN id ELSE NULL END ) count
FROM
( SELECT id, NAME, ROW_NUMBER() over ( PARTITION BY id ORDER BY id ) rn FROM test ) tmp
版权声明
本文为[解忧杂货铺Q]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_26003101/article/details/124331663
边栏推荐
- 上海航芯技术分享 | ACM32 MCU安全特性概述
- 【BIM入门实战】Revit建筑墙体:构造、包络、叠层图文详解
- Definition, understanding and calculation of significant figures in numerical analysis
- 作为一名码农,女友比自己更能码是一种什么体验?
- Cause analysis of incorrect time of AI traffic statistics of Dahua Equipment Development Bank
- How to introduce opencv into cmake project
- Mysql---数据读写分离、多实例
- 无线充电全国产化电子元件推荐方案
- [string] ranking of country names ----- problem solving notes
- AI CC 2019 installation tutorial under win10 (super detailed - small white version)
猜你喜欢
[AI vision · quick review of robot papers today, issue 30] Thu, 14 APR 2022
ROS series (IV): ROS communication mechanism series (6): parameter server operation
As a code farmer, what kind of experience is it that a girlfriend can code better than herself?
Hard core chip removal
阿里云IoT流转到postgresql数据库方案
【BIM+GIS】ArcGIS Pro2. 8 how to open Revit model, Bim and GIS integration?
ROS series (III): introduction to ROS architecture
Network principle | connection management mechanism in TCP / IP important protocol and core mechanism
Thought of reducing Governance -- detailed summary of binary search
matlab读取多张fig图然后合并为一张图(子图的形式)
随机推荐
The super large image labels in remote sensing data set are cut into specified sizes and saved into coco data set - target detection
ERROR: Could not find a version that satisfies the requirement win32gui
[AI vision · quick review of NLP natural language processing papers today, issue 30] Thu, 14 APR 2022
MySQL 2013 lost connection to MySQL server during query
Summary of knowledge map (I)
Network principle | connection management mechanism in TCP / IP important protocol and core mechanism
Express middleware ② (classification of Middleware)
Definition, understanding and calculation of significant figures in numerical analysis
[AI vision · quick review of today's sound acoustic papers issue 1] Thu, 14 APR 2022
【测绘程序设计】坐标反算神器V1.0(附C/C#/VB源程序)
[BIM introduction practice] wall hierarchy and FAQ in Revit
ROS series (4): ROS communication mechanism series (4): topic communication practice
[mapping program design] coordinate inverse artifact v1 0 (with C / C / VB source program)
Basic introduction to spot gold
[string] ranking of country names ----- problem solving notes
Xiaomi, which has set the highest sales record of domestic mobile phones in overseas markets, paid renewed attention to the domestic market
Cuda11 is installed perfectly in win10 X + pytorch 1.9 (blood flowing into the river) cuda. is_ Available() becomes true!
阿里云IoT流转到postgresql数据库方案
【ICCV 2019】MAP-VAE:Multi-Angle Point Cloud-VAE: Unsupervised Feature Learning for 3D Point Clouds..
【Echart】echart 入门