
DISTINCT关键字与SELECT语句一起使用,用于去除重复记录,只获取唯一的记录。
当一个表中有多个重复记录,当提取这样的记录时,DISTINCT关键字就显得特别有意义,它只获取唯一一次记录,而不是获取重复记录。
语法格式
""SELECT DISTINCT [ ON ( expression [, ...] ) ] ]
{ * | [column, ...] }
[ FROM from_item [, ...] ];
参数说明
- DISTINCT [ ON ( expression [, …] ) ]
从SELECT的结果集中删除所有重复的行,使结果集中的每行都是唯一的。
ON ( expression [, …] ) 只保留那些在给出的表达式上运算出相同结果的行集合中的第一行。
示例
在表customer_t1中插入两条数据:
""openGauss=# INSERT INTO customer_t1 (c_customer_sk, c_customer_id, c_first_name,Amount) VALUES
(6881, 'maps', 'Lily',1000),
(4320, 'tpcds', 'Lily',2000);
现在数据如下:
""openGauss=# SELECT * FROM customer_t1 ;
c_customer_sk | c_customer_id | c_first_name | c_last_name | amount
---------------+---------------+--------------+-------------+--------
3869 | hello | Grace | | 1000
3869 | hello | Grace | | 1000
3869 | | Grace | |
3869 | hello | | |
3869 | hello | | |
| | | |
6985 | maps | Joes | | 2200
9976 | world | James | | 5000
4421 | Admin | Local | | 3000
6881 | maps | Lily | | 1000
4320 | tpcds | Lily | | 2000
(11 rows)
查询customer_t1表中所有的c_first_name。其中存在两个Lily和三个Grace。
""openGauss=# SELECT c_first_name FROM customer_t1 ;
c_first_name
--------------
Grace
Grace
Grace
Joes
James
Local
Lily
Lily
(11 rows)
在SELECT语句中使用DISTINCT关键字。从结果中可以发现,重复数据已经被删除。
""openGauss=# SELECT DISTINCT c_first_name FROM customer_t1 ;
c_first_name
--------------
James
Grace
Local
Joes
Lily
(6 rows)「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




