当前位置:网站首页>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
边栏推荐
- Alibaba cloud IOT transfer to PostgreSQL database scheme
- STM32F4单片机ADC采样及ARM-DSP库的FFT
- CRF based medical entity recognition baseline
- Basic usage of Google colab (I)
- ROS series (IV): ROS communication mechanism series (1): topic communication
- Cuda11 is installed perfectly in win10 X + pytorch 1.9 (blood flowing into the river) cuda. is_ Available() becomes true!
- [AI vision · quick review of robot papers today, issue 28] wed, 1 Dec 2021
- Hard core chip removal
- VHDL语言实现32位二进制数转BCD码
- QT program integration easyplayer RTSP streaming media player screen flicker what is the reason?
猜你喜欢

单极性非归零NRZ码、双极性非归零NRZ码、2ASK、2FSK、2PSK、2DPSK及MATLAB仿真
![[BIM introduction practice] wall hierarchy and FAQ in Revit](/img/95/e599c7547029f57ce23ef4b87e8b9a.jpg)
[BIM introduction practice] wall hierarchy and FAQ in Revit

【测绘程序设计】坐标反算神器V1.0(附C/C#/VB源程序)

Solve the technical problems in seq2seq + attention machine translation

为什么推荐你学嵌入式

Process seven state transition diagram
![[AI vision · quick review of robot papers today, issue 28] wed, 1 Dec 2021](/img/c8/90d020d192fe791c4dec5f4161e597.png)
[AI vision · quick review of robot papers today, issue 28] wed, 1 Dec 2021

STM32单片机ADC规则组多通道转换-DMA模式

Introduction to Cortex-M3 register set, assembly language and C language interface

Qtspim manual - Chinese Translation
随机推荐
Nel ASA:挪威Herøya设施正式启用
Vscode download and installation + running C language
ROS series (III): introduction to ROS architecture
PolarMask is not in the models registry
现货黄金操作技巧_估波曲线
Mysql---数据读写分离、多实例
As a code farmer, what kind of experience is it that a girlfriend can code better than herself?
Xiaomi, qui a établi le plus grand volume de ventes de téléphones portables domestiques sur le marché d'outre - mer, se concentre de nouveau sur le marché intérieur
创下国产手机在海外市场销量最高纪录的小米,重新关注国内市场
Operating skills of spot gold_ Wave estimation curve
Why recommend you to study embedded
[AI vision · quick review of robot papers today, issue 30] Thu, 14 APR 2022
Set classic topics
作为一名码农,女友比自己更能码是一种什么体验?
Express中间件②(中间件的分类)
秒杀所有区间相关问题
【BIM入门实战】Revit中的墙体层次以及常见问题解答
STM32单片机ADC规则组多通道转换-DMA模式
ROS series (IV): ROS communication mechanism series (3): parameter server
MySQL 2013 lost connection to MySQL server during query