sql

sql中distinct函数的作用是什么

小亿
85
2023-12-14 22:38:19
栏目: 云计算

在SQL中,DISTINCT函数的作用是返回不重复的行/记录。它应用于SELECT语句中的列,用来排除重复的值,只返回唯一的值。

例如,假设有一个名为"customers"的表,包含以下数据:

customer_id customer_name city
1 John London
2 Mary Paris
3 John Berlin
4 David London

如果我们执行以下SQL查询:

SELECT DISTINCT customer_name, city FROM customers;

结果将会是:

customer_name city
John London
Mary Paris
David London
John Berlin

可以看到,DISTINCT函数只返回唯一的customer_name和city组合,去除了重复的行。

0
看了该问题的人还看了