当前位置:网站首页>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
边栏推荐
- [mapping program design] coordinate inverse artifact v1 0 (with C / C / VB source program)
- Solve the technical problems in seq2seq + attention machine translation
- 【NeurIPS 2019】Self-Supervised Deep Learning on Point Clouds by Reconstructing Space
- 【BIM入门实战】Revit建筑墙体:构造、包络、叠层图文详解
- 为什么推荐你学嵌入式
- 单片机串口数据处理(1)——串口中断发送数据
- 【BIM入门实战】Revit中的墙体层次以及常见问题解答
- Zotero6. Version 0 quicklook cannot be used / Chinese garbled code will not be displayed
- [BIM introduction practice] wall hierarchy and FAQ in Revit
- Unipolar NRZ code, bipolar NRZ code, 2ASK, 2FSK, 2PSK, 2DPSK and MATLAB simulation
猜你喜欢

matlab讀取多張fig圖然後合並為一張圖(子圖的形式)

QT program integration easyplayer RTSP streaming media player screen flicker what is the reason?

STM32 upper μ C / shell transplantation and Application

Cuda11 is installed perfectly in win10 X + pytorch 1.9 (blood flowing into the river) cuda. is_ Available() becomes true!

Let matlab2018b support the mex configuration of vs2019
![[mapping program design] coordinate azimuth calculation artifact (version C)](/img/2b/e640f3e2702f80d003fa3dd5c4158d.png)
[mapping program design] coordinate azimuth calculation artifact (version C)

Win10 boot VMware virtual machine boot seconds blue screen problem perfect solution

指纹Key全国产化电子元件推荐方案

一个函数秒杀2Sum 3Sum 4Sum问题

【测绘程序设计】坐标方位角推算神器(C#版)
随机推荐
Matlab minimalist configuration of vscode configuration
作为一名码农,女友比自己更能码是一种什么体验?
Retrieval question answering system baseline
Using VBA interval to extract one column from another in Excel
Summary of knowledge map (3)
【BIM+GIS】ArcGIS Pro2. 8 how to open Revit model, Bim and GIS integration?
[BIM introduction practice] wall hierarchy and FAQ in Revit
How Zotero quotes in word jump to references / hyperlink
MATLAB lit plusieurs diagrammes fig et les combine en un seul diagramme (sous forme de sous - Diagramme)
VSCode配置之Matlab极简配置
Stm32f4 MCU ADC sampling and FFT of ARM-DSP Library
【测绘程序设计】坐标方位角推算神器(C#版)
[AI vision · quick review of NLP natural language processing papers today, issue 31] Fri, 15 APR 2022
ROS series (IV): ROS communication mechanism series (5): Service Communication Practice
Jupiter notebook modify configuration file setting startup directory is invalid
The difference between lists, tuples, dictionaries and collections
【Echart】echart 入門
小红书被曝整体裁员20%,大厂之间内卷也很严重
STM32 MCU ADC rule group multi-channel conversion DMA mode
[AI vision · quick review of NLP natural language processing papers today, issue 29] Mon, 14 Feb 2022