当前位置:网站首页>面试被问MySQL 如何去重,还有谁不会?
面试被问MySQL 如何去重,还有谁不会?
2022-07-19 11:33:00 【InfoQ】

- task_id: 任务id;
- order_id: 订单id;
- start_time: 开始时间
distinct
-- 列出 task_id 的所有唯一值(去重后的记录)
-- select distinct task_id
-- from Task;
-- 任务总数
select count(distinct task_id) task_num
from Task;
group by
-- 列出 task_id 的所有唯一值(去重后的记录,null也是值)
-- select task_id
-- from Task
-- group by task_id;
-- 任务总数
select count(task_id) task_num
from (select task_id
from Task
group by task_id) tmp;
row_number
row_number() over (partition by <用于分组的字段名> order by <用于组内排序的字段名>)
-- 在支持窗口函数的 sql 中使用
select count(case when rn=1 then task_id else null end) task_num
from (select task_id
, row_number() over (partition by task_id order by start_time) rn
from Task) tmp;

-- 下方的分号;用来分隔行
select distinct user_id
from Test; -- 返回 1; 2
select distinct user_id, user_type
from Test; -- 返回1, 1; 1, 2; 2, 1
select user_id
from Test
group by user_id; -- 返回1; 2
select user_id, user_type
from Test
group by user_id, user_type; -- 返回1, 1; 1, 2; 2, 1
select user_id, user_type
from Test
group by user_id;
-- Hive、Oracle等会报错,mysql可以这样写。
-- 返回1, 1 或 1, 2 ; 2, 1(共两行)。只会对group by后面的字段去重,就是说最后返回的记录数等于上一段sql的记录数,即2条
-- 没有放在group by 后面但是在select中放了的字段,只会
边栏推荐
- 一文详解scp命令
- 华泰证券账户开通流程,手机上开户安全吗
- 百问百答第47期:极客有约——中信建投当前的监控体系建设情况
- Alibaba hot updates the story of sophix
- PL-VIO: Tightly-Coupled Monocular Visual–Inertial Odometry Using Point and Line Features
- oracle查看执行最慢与查询次数最多的sql语句
- Performance area: the more you know, the more you don't know
- 线程池,我是谁?我在哪儿?
- 在线随机抛硬币正反面统计工具
- Mysql引擎介绍及InnoDB逻辑存储结构
猜你喜欢
Too busy or too lazy? The same fund manager has the same view in the quarterly reports of different products
NetApp 扩展柜 Disk Shelf 扩容方法步骤
国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐
尚乘数科美股IPO:市值30亿美元 蔡志坚收获第二家上市企业
USART串口通讯
Cooperatively Coevolving Particle Swarms forLarge Scale Optimization
NetApp expansion enclosure disk shelf expansion method steps
【Latex】PPT畫圖,導出emf格式,word插入emf文件並導出pdf,pdf裁剪並導出eps文件,latex插入eps文件
How to use SAP intelligent robotic process automation to automate Excel
云原生、Intel Arch及云原生机密计算 3 大 SIG 在线分享!今天见 | 第 32-34 期
随机推荐
【历史上的今天】7 月 18 日:英特尔成立;万维网上传了第一张照片;eBay 分拆 PayPal
股票开户佣金特惠炒股佣金最低的证券公司,手机上开户安不安全
LINK : fatal error LNK1104: 无法打开文件“ucrtd.lib” 解决方法 Visual Studio
MySQL主从复制数据同步,常见问题总结
Typescript foundation interface interface
C#/VB.NET 添加多行文本水印到Word文档
Quelles sont les principales techniques et pratiques de redis pour soutenir les scènes de second kill?
[latex] ppt drawing, exporting EMF format, word inserting EMF file and exporting PDF, PDF clipping and exporting EPS file, latex inserting EPS file
SourceTree推送时没有远程分支
Go语言 Go语言内置容器
在线XML转JSON工具
day04_数组
定价随心、产品难辨真假、平台跑路,数藏市场还会火下去吗?
简单的注册登录的实现
Mysql database
SAP 实施项目中涉及到编程方式操作 Excel 的几种场景介绍
Cooperatively Coevolving Particle Swarms forLarge Scale Optimization
8、ORM简介与gorm入门
Golang语言快速上手到综合实战笔记(Go语言、Beego框架、高并发聊天室、爬虫)
clickhouse笔记02--安装测试 clickvisual