sql語句distinct
Hey, folks! In this article, we will we focusing on SQL SELECT DISTINCT statement.
sql語句幾個并列條件, 嘿伙計! 在本文中,我們將重點介紹SQL SELECT DISTINCT語句 。
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 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子句有助于從表中提取非冗余值,即它排除了冗余/重復值,并且僅顯示唯一值 。
Syntax:
句法:
DISTINCT(column-name)
Example:
例:
SELECT DISTINCT(city) from Info;
Output:
輸出:
city
Pune
Satara
Mumbai
USA
SQL SELECT statement
can be clubbed along with DISTINCT clause
to extract and display the unique values from a particular table.
可以將SQL SELECT statement
與DISTINCT 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語句的工作原理之后,現在讓我們在下面的部分中了解一下該語句的實現。
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
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 。
翻譯自: https://www.journaldev.com/41332/sql-select-distinct-statement
sql語句distinct
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态