sql语句distinct

Hey, folks! In this article, we will we focusing on SQL SELECT DISTINCT statement.

嘿伙计! 在本文中,我们将重点介绍SQL SELECT DISTINCT语句



什么是SQL SELECT语句? (What is SQL SELECT statement?)

SQL Select statement is used to select and display particular data from the column of a table/database.

SQL Select语句用于从表/数据库的列中选择和显示特定数据。

Syntax:

句法:


SELECT * FROM Table-name;
OR
SELECT column1, column2, ...., columnN from Table-name;

Example:

例:


create table Info(id integer, Cost integer, city varchar(200));
insert into Info(id, Cost,city) values(1, 100,"Pune");
insert into Info(id, Cost,city) values(2, 50, "Satara");
insert into Info(id, Cost,city) values(3, 65,"Pune");
insert into Info(id, Cost,city) values(4, 97,"Mumbai");
insert into Info(id, Cost,city) values(5, 12,"USA");
Select * from Info;

Output:

输出:


id  Cost    city
1   100 Pune
2   50  Satara
3   65  Pune
4   97  Mumbai
5   12  USA


什么是SQL DISTINCT子句? (What is SQL DISTINCT Clause?)

SQL DISTINCT clause helps extract the non-redundant values from the table i.e. it excludes the redundant/repeated values and displays only the unique values.

SQL DISTINCT子句有助于从表中提取非冗余值,即它排除了冗余/重复值,并且仅显示唯一值

  • DISTINCT clause works well with SQL Aggregation functions.DISTINCT子句与SQL Aggregation函数配合良好 。
  • Multiple columns cannot be mapped at once by SQL DISTINCT clause.SQL DISTINCT子句不能一次映射多个列。

Syntax:

句法:


DISTINCT(column-name)

Example:

例:


SELECT DISTINCT(city) from Info;

Output:

输出:


city
Pune
Satara
Mumbai
USA


使用DISTINCT子句SQL SELECT的工作 (Working of SQL SELECT with DISTINCT clause)

SQL SELECT statement can be clubbed along with DISTINCT clause to extract and display the unique values from a particular table.

可以将SQL SELECT statementDISTINCT clause在一起,以提取和显示特定表中的唯一值。

Moreover, the SELECT DISTINCT statement can be used along with different aggregation functions such as COUNT(), AVG(), etc to display the unique values according to certain predefined conditions.

此外,SELECT DISTINCT语句可以与不同的聚合函数(例如COUNT(),AVG()等)一起使用 ,以根据某些预定义条件显示唯一值。

Syntax:

句法:


SELECT DISTINCT(column-name)
FROM Table;

Having understood the working of SQL SELECT DISTINCT statement, let us now have a look at the implementation of the same in the below section.

了解了SQL SELECT DISTINCT语句的工作原理之后,现在让我们在下面的部分中了解一下该语句的实现。



通过示例实现SQL SELECT DISTINCT (Implementing SQL SELECT DISTINCT through examples)

In the below example, we have clubbed SQL DISTINCT statement along with ORDER BY clause. Wherein, it displays the unique ‘city’ data in an ascending order according to the Cost values.

在下面的示例中,我们将SQL DISTINCT语句与ORDER BY子句结合在一起。 其中,它将根据“成本”值以升序显示唯一的“城市”数据。


Select DISTINCT(city),Cost
From Info
ORDER BY Cost;

Output:

输出:


city    Cost
USA 12
Satara  50
Pune    65
Mumbai  97
Pune    100

Now, we have used COUNT(*) function along with SELECT DISTINCT statement to display the count of all the unique values of the column — ‘city’.

现在,我们已经使用COUNT(*) function和SELECT DISTINCT语句来显示列“ city”的所有唯一值的计数。


SELECT COUNT(*)
FROM (
SELECT  DISTINCT city
FROM Info) as Inn;

Output:

输出:


COUNT(*)
4

Here, we have created a Table — ‘Info’ with various columns. Further, we have used SELECT DISTINCT statement along with WHERE clause to extract and display the unique values whose ‘Cost’ is equal to 100.

在这里,我们用不同的列创建了一个表-'Info'。 此外,我们将SELECT DISTINCT语句与WHERE子句一起使用,以提取并显示“成本”等于100的唯一值。


create table Info(id integer, Cost integer, city varchar(200));
insert into Info(id, Cost,city) values(1, 100,"Pune");
insert into Info(id, Cost,city) values(2, 50, "Satara");
insert into Info(id, Cost,city) values(3, 100,"Pune");
insert into Info(id, Cost,city) values(4, 100,"Mumbai");
insert into Info(id, Cost,city) values(5, 12,"USA");
SELECT DISTINCT city,Cost
FROM Info
WHERE Cost=100;

Output:

输出:


city    Cost
Pune    100
Mumbai  100


结论 (Conclusion)

By this, we have come to the end of this topic. Feel free to comment below in case you come across any doubt.

至此,我们到了本主题的结尾。 如果您有任何疑问,请在下面发表评论。

For more such topics related to SQL, please do visit SQL JournalDev.

有关与SQL有关的更多此类主题,请访问SQL JournalDev 。



参考资料 (References)

  • SQL SELECT DISTINCT — DocumentationSQL SELECT DISTINCT —文档

翻译自: https://www.journaldev.com/41332/sql-select-distinct-statement

sql语句distinct

sql语句distinct_带DISTINCT子句SQL SELECT语句相关推荐

  1. GBase 8s SQL 指南:教程———6编写高级SELECT语句

    6编写高级SELECT语句 本章中增大了使用SELECT语句可执行的操作的范围.并使您能够执行更复杂的数据库查 询和数据处理.编写SELECT语句着重于SELECT语句语法中的五个子句.本章添加了 G ...

  2. mysql的selectwhere语句_(MySQL笔记)SELECT语句的WHERE子句详解

    WHERE子句 在SELECT语句中,语句首先从FROM子句开始执行,执行后会生成一个中间结果集,然后就开始执行WHERE子句.WHERE子句是对FROM子句生成的结果集进行过滤,对中间结果集的每一行 ...

  3. 《C++游戏编程入门(第4版)》——2.4 使用带else子句的if语句序列

    本节书摘来自异步社区出版社<C++游戏编程入门(第4版)>一书中的第2章,第2.4节,作者:[美]Michael Dawson(道森),更多章节内容可以访问云栖社区"异步社区&q ...

  4. 零基础带你学习MySQL—Select语句以及注意事项(十)

    零基础带你学习MySQL-Select语句以及注意事项(十)

  5. 11.32-投影与select语句 11.33-选择与select语句 11.34-笛卡尔积与select语句 11.35-θ连接与select语句 11.36-自然连接与select语句

    目录 一.投影与select语句 二.选择与select语句 三.笛卡尔积与select语句 四.θ连接与select语句 五.自然连接与select语句 六.考点:关系代数运算与对应的select查 ...

  6. SQL的INSERT INTO和INSERT INTO SELECT语句

    INSERT INTO语句用来给一个table插入信息的records. 语法: 第一种,指定列名和插入的值 INSERT INTO table_name (column1, column2, col ...

  7. 【如何成为SQL高手】第五关:select语句基本用法

  8. mysql的group语句_MySQL中distinct与group by语句的一些比较及用法讲解

    这篇文章主要介绍了MySQL中distinct与group by的一些比较及用法讲解,二者在查询操作中的用法有很多相似之处,需要的朋友可以参考下 在数据表中记录了用户验证时使用的书目,现在想取出所有书 ...

  9. python for语句 不带冒号_为什么Python语句块要有一个分号(:)?

    我们都知道,Python的 if.for.while.def 语句第一行行尾需要加一个冒号,就像这样: if a == 1: b = 2 但是Python是依据缩进来判断代码层次的,那么这个分号是否是 ...

最新文章

  1. Android使用smack连接openfire(本地+远程)
  2. Redis详解(一)------ redis的简介与安装
  3. java模块是什么6_Java 9 揭秘(6. 封装模块)
  4. 开源播放器 支持视频广告
  5. Android静态代码扫描效率优化与实践
  6. 怎么卸载python 旧版本_Python 如何移除旧的版本特性,如何迎接新的特性?
  7. 程序员职业自由6个阶段,你处于哪个?
  8. 中文select 加载完成后再排序
  9. 【JVM】 讲的比较好的JVM博客连接
  10. bzoj 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚(DP)
  11. linux oracle 运维_运维日记| Linux磁盘空间被莫名占用的问题
  12. 数学建模——数据分析方法
  13. iwconfig命令
  14. python贴吧爬虫-python贴吧爬虫
  15. 神经网络和深度学习(一)——初识神经网络
  16. fastadmin 邮件配置
  17. java虚拟机假死怎么办_虚拟机假死解决方法
  18. 《创业时代》,不带你们这样黑程序员的
  19. 树莓派 Raspberry 4B 刷机、上网、录音外设、文件传输、电脑投屏问题汇总
  20. 物体检测之SNIPER

热门文章

  1. 《算法分析与设计》课程任务
  2. centos7安装rabbitmq 总结
  3. Go语言的基准测试简单示例
  4. [bzoj 1452] [JSOI2009]Count
  5. python基础补漏-03-函数
  6. 转载:网关的概念以及形象的比喻
  7. [转载] numpy.argmin 使用
  8. [转载] numpy.gradient
  9. JavaEE——Mybatis(6)--association的分步查询与延迟加载 以及Collection和鉴别器discriminator...
  10. transient、Externalizable