PostgreSQL DISTINCT 关键字与SELECT语句结合使用,可消除所有重复记录并仅提取唯一记录。
Distinct - 语法
消除重复记录的DISTINCT关键字的基本语法如下-
SELECT DISTINCT column1, column2,.....columnN FROM table_name WHERE [condition]
Distinct - 示例
考虑表 COMPANY 具有以下记录:
# select * from COMPANY; id | name | age | address | salary ----+-------+-----+-----------+-------- 1 | Paul | 32 | California| 20000 2 | Learnfk | 25 | Texas | 15000 3 | Teddy | 23 | Norway | 20000 4 | Mark | 25 | Rich-Mond | 65000 5 | David | 27 | Texas | 85000 6 | Kim | 22 | South-Hall| 45000 7 | James | 24 | Houston | 10000 (7 rows)
让无涯教程向该表中再添加两个记录,如下所示:
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (8, 'Paul', 32, 'California', 20000.00 ); INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (9, 'Learnfk', 25, 'Texas', 15000.00 );
现在,COMPANY表中的记录将是-
id | name | age | address | salary ----+-------+-----+------------+-------- 1 | Paul | 32 | California | 20000 2 | Learnfk | 25 | Texas | 15000 3 | Teddy | 23 | Norway | 20000 4 | Mark | 25 | Rich-Mond | 65000 5 | David | 27 | Texas | 85000 6 | Kim | 22 | South-Hall | 45000 7 | James | 24 | Houston | 10000 8 | Paul | 32 | California | 20000 9 | Learnfk | 25 | Texas | 15000 (9 rows)
首先,让无涯教程看看以下SELECT查询如何返回重复的薪水记录-
testdb=# SELECT name FROM COMPANY;
这将产生以下输出-
name ------- Paul Learnfk Teddy Mark David Kim James Paul Learnfk (9 rows)
现在,让无涯教程在上面的SELECT查询中使用 DISTINCT 关键字,然后查看输出-
testdb=# SELECT DISTINCT name FROM COMPANY;
如果无涯教程没有任何重复的条目,这将产生以下输出-
name ------- Teddy Paul Mark David Learnfk Kim James (7 rows)
参考链接
https://www.learnfk.com/postgresql/postgresql-distinct-keyword.html
标签:25,PostgreSQL,name,Distinct,COMPANY,无涯,Paul,Learnfk,Texas From: https://blog.51cto.com/u_14033984/8957005