开发者学堂课程【MySQL数据库入门学习:select distinct去掉重复查询结果】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/451/detail/5575
select distinct 去掉重复查询结果
distinct(精确的)
select distinct col_name from table_name;
与 select 语句的唯一区别就是多了 distinct 语句,distinct 意思是精确的
distinct 作用:去掉查询出来的大量重复数据记录,重复数据只会产生一次。
例子:
select distinct title from book;
与之前讲的 select 的使用方法没有区别。
演示:
mysql>select *from book;
id title content
1 t hah contwnt
NULL title NULL
3 t c
NULL NULL c
4 t c
查询 book 表中的title
mysql>select title from book;
title
t hah
title
t
NULL
t
只查出不重复的 title(distinct 的作用)
mysql>select distinct title from book;
title
t hah
title
t
NULL
distinct 不止作用于单列上,还可以作用于多列的数据组合中。
查询 title 和 content 的组合
mysql>select title,content from book;
title content
t hah content
title1 NULL
t c
NULL c
t c
只查出不重复的 title 和 content 的组合(distinct 的作用)
mysql>select distinct title,content from book;
title content
t hah content
title1 NULL
t c
总结:
distinct 命令的作用:在数据量很大时,查看数据是删除重复数据。