sql中distinct函數(shù)的作用是什么

sql
小億
90
2023-12-14 22:38:19
欄目: 云計(jì)算

在SQL中,DISTINCT函數(shù)的作用是返回不重復(fù)的行/記錄。它應(yīng)用于SELECT語句中的列,用來排除重復(fù)的值,只返回唯一的值。

例如,假設(shè)有一個(gè)名為"customers"的表,包含以下數(shù)據(jù):

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

如果我們執(zhí)行以下SQL查詢:

SELECT DISTINCT customer_name, city FROM customers;

結(jié)果將會(huì)是:

customer_name city
John London
Mary Paris
David London
John Berlin

可以看到,DISTINCT函數(shù)只返回唯一的customer_name和city組合,去除了重復(fù)的行。

0