当前位置:网站首页>MySQL教程67-使用DISTINCT过滤重复数据
MySQL教程67-使用DISTINCT过滤重复数据
2022-07-25 15:34:00 【51CTO】
在 MySQL 中使用 SELECT 语句执行简单的数据查询时,返回的是所有匹配的记录。如果表中的某些字段没有唯一性约束,那么这些字段就可能存在重复值。为了实现查询不重复的数据,MySQL 提供了 DISTINCT 关键字。
DISTINCT 关键字的主要作用就是对数据表中一个或多个字段重复的数据进行过滤,只返回其中的一条数据给用户。
DISTINCT 关键字的语法格式为:
其中,“字段名”为需要消除重复记录的字段名称,多个字段时用逗号隔开。
使用 DISTINCT 关键字时需要注意以下几点:
- DISTINCT 关键字只能在 SELECT 语句中使用。
- 在对一个或多个字段去重时,DISTINCT 关键字必须在所有字段的最前面。
- 如果 DISTINCT 关键字后有多个字段,则会对多个字段进行组合去重,也就是说,只有多个字段组合起来完全是一样的情况下才会被去重。
例 1
下面通过一个具体的实例来说明如何实现查询不重复数据。
test 数据库中 student 表的表结构和数据如下所示:
结果显示,student 表中存在 6 条记录。
下面对 student 表的 age 字段进行去重,SQL 语句和运行结果如下:
对 student 表的 name 和 age 字段进行去重,SQL 语句和运行结果如下:
对 student 表中的所有字段进行去重,SQL 语句和运行结果如下:
因为 DISTINCT 只能返回它的目标字段,而无法返回其它字段,所以在实际情况中,我们经常使用 DISTINCT 关键字来返回不重复字段的条数。
查询 student 表中对 name 和 age 字段去重之后记录的条数,SQL 语句和运行结果如下:
结果显示,student 表中对 name 和 age 字段去重之后有 5 条记录。
边栏推荐
- 哪里有搭建flink cdc抽mysql数的demo?
- Pytorch学习笔记-Advanced_CNN(Using Inception_Module)实现Mnist数据集分类-(注释及结果)
- MySQL—用户和权限管控
- Solve the vender-base.66c6fc1c0b393478adf7.js:6 typeerror: cannot read property 'validate' of undefined problem
- Pytoch framework exercise (based on kaggle Titanic competition)
- LeetCode - 362 敲击计数器(设计)
- Idea - click the file code to automatically synchronize with the directory
- GAMES101复习:三维变换
- Leetcode - 641 design cycle double ended queue (Design)*
- Understand "average load"
猜你喜欢

Redis distributed lock, it's really impossible without it

CircleIndicator组件,使指示器风格更加多样化

Leetcode - 379 telephone directory management system (Design)

GAMES101复习:变换

Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output

Pytorch学习笔记--常用函数总结3

Pytorch学习笔记--SEResNet50搭建

华为2023届提前批预热开始!左 神的程序代码面试指南终派上用场

Leetcode - 232 realize queue with stack (design double stack to realize queue)

Pytorch学习笔记-Advanced_CNN(Using Inception_Module)实现Mnist数据集分类-(注释及结果)
随机推荐
2021 Shanghai match-h-two point answer
Flink-1.13.6版本的 Flink sql以yarn session 模式运行,怎么禁用托管
Geogle colab notes 1-- run the.Py file on the cloud hard disk of Geogle
Pytoch learning notes - Teacher Liu Er RNN advanced chapter - code comments and results
Redis distributed lock, it's really impossible without it
Beyond Compare 4 实现class文件对比【最新】
CF365-E - Mishka and Divisors,数论+dp
The difference between mouseover and mouseenter
Leetcode - 677 key value mapping (Design)*
LeetCode - 303 区域和检索 - 数组不可变 (设计 前缀和数组)
Leetcode - 641 design cycle double ended queue (Design)*
ZOJ - 4114 flipping game DP, reasonable state representation
MySQL optimization summary II
「数字安全」警惕 NFT的七大骗局
Pytoch learning notes advanced_ CNN (using perception_module) implements MNIST dataset classification - (comments and results)
2021 Shanghai sai-d-cartland number variant, DP
Where is there a demo to set up the flex CDC to draw the number of MySQL?
2021上海市赛-B-排序后dp
Matlab simulation of BPSK modulation system (1)
微信小程序