当前位置:网站首页>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
边栏推荐
- Express中间件②(中间件的分类)
- 智能电子秤全国产化电子元件推荐方案
- [AI vision · quick review of robot papers today, issue 28] wed, 1 Dec 2021
- Digital image processing third edition Gonzalez notes Chapter 2
- MySQL 2013 lost connection to MySQL server during query
- What if you encounter symbols you don't know in mathematical formulas
- 【测绘程序设计】坐标方位角推算神器(C#版)
- Cause analysis of incorrect time of AI traffic statistics of Dahua Equipment Development Bank
- [AI vision · quick review of NLP natural language processing papers today, issue 28] wed, 1 Dec 2021
- Zotero6. Version 0 quicklook cannot be used / Chinese garbled code will not be displayed
猜你喜欢
ROS series (III): introduction to ROS architecture
【ICCV 2019】MAP-VAE:Multi-Angle Point Cloud-VAE: Unsupervised Feature Learning for 3D Point Clouds..
小红书被曝整体裁员20%,大厂之间内卷也很严重
Overview of knowledge map (II)
Introduction to Cortex-M3 register set, assembly language and C language interface
[AI vision · quick review of NLP natural language processing papers today, No. 32] wed, 20 APR 2022
[AI vision · quick review of NLP natural language processing papers today, issue 31] Fri, 15 APR 2022
Writing latex with vscode - the latest tutorial 2022 / 4 / 17
Retrieval question answering system baseline
为什么推荐你学嵌入式
随机推荐
/etc/bash_completion.d目录作用(用户登录立刻执行该目录下脚本)
列表、元组、字典和集合的区别
C语言 字符常量
Nel ASA: her ø Ya facility in Norway officially opened
兼容NSR20F30NXT5G的小体积肖特基二极管
[AI vision · quick review of NLP natural language processing papers today, issue 31] Fri, 15 APR 2022
【ICCV 2019】MAP-VAE:Multi-Angle Point Cloud-VAE: Unsupervised Feature Learning for 3D Point Clouds..
What is software acceptance testing? What are the benefits of acceptance testing conducted by third-party software testing institutions?
KVM error: Failed to connect socket to ‘/var/run/libvirt/libvirt-sock‘
【BIM入门实战】Revit中的墙体层次以及常见问题解答
QtSpim手册-中文翻译
[echart] Introduction to echart
Xiaomi, which has set the highest sales record of domestic mobile phones in overseas markets, paid renewed attention to the domestic market
[mathematical modeling] my mathematical memory
2021-09-03 crawler template (only static pages are supported)
知乎有问题,谁来解答?
单极性非归零NRZ码、双极性非归零NRZ码、2ASK、2FSK、2PSK、2DPSK及MATLAB仿真
Opencv -- yoact case segmentation model reasoning
Matlab reads multiple fig graphs and then combines them into one graph (in the form of sub graph)
ROS series (III): introduction to ROS architecture