sql語句幾個并列條件,sql語句distinct_帶DISTINCT子句SQL SELECT語句

 2023-11-19 阅读 12 评论 0

摘要:sql語句distinctHey, folks! In this article, we will we focusing on SQL SELECT DISTINCT statement.sql語句幾個并列條件, 嘿伙計! 在本文中,我們將重點介紹SQL SELECT DISTINCT語句 。 什么是SQL SELECT語句? (What is SQL SELECT statement?) SQL

sql語句distinct

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

sql語句幾個并列條件, 嘿伙計! 在本文中,我們將重點介紹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語句用于從表/數據庫的列中選擇和顯示特定數據。

sql case語句,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 — Documentation

    SQL SELECT DISTINCT —文檔

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

sql語句distinct

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/182870.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息