例子
考虑具有以下记录的表 COMPANY -
# select * from COMPANY;
id | name | age | address | salary
----+-------+-----+-----------+--------
1 | Paul | 32 | California| 20000
2 | Allen | 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, 'Allen', 25, 'Texas', 15000.00 );
现在,COMPANY 表中的记录将是 -
id | name | age | address | salary
----+-------+-----+------------+--------
1 | Paul | 32 | California | 20000
2 | Allen | 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 | Allen | 25 | Texas | 15000
(9 rows)
首先,让我们看看以下 SELECT 查询如何返回重复的工资记录 -
testdb=# SELECT name FROM COMPANY;
这将产生以下结果 -
name
-------
Paul
Allen
Teddy
Mark
David
Kim
James
Paul
Allen
(9 rows)
现在,让我们使用DISTINCT带有上述 SELECT 查询的关键字并查看结果 -
testdb=# SELECT DISTINCT name FROM COMPANY;
在我们没有任何重复条目的情况下,这将产生以下结果 -
name
-------
Teddy
Paul
Mark
David
Allen
Kim
James
(7 rows)