Home Backend Development PHP Tutorial sql distinct关键字有什么用?

sql distinct关键字有什么用?

Jun 13, 2016 pm 12:10 PM
distinct sql

sql distinct关键字有什么用?

在表中,一个列可能会包含多个重复值,有时您也许希望仅仅列出不同(distinct)的值。

DISTINCT 关键词用于返回唯一不同的值。

在SELECT子句中,可以通过指明DISTINCT关键字,来去除数据列中的重复信息,返回唯一不同的值。

SQL SELECT DISTINCT 语法

SELECT DISTINCT column_name,column_name
FROM table_name;
Copy after login

示例:

1、只对一个字段查重

对一个字段查重,表示选取该字段一列不重复的数据。

示例表: psur_list

1.png

PLAN_NUMBER字段去重,语句:

SELECT DISTINCT PLAN_NUMBER  FROM psur_list;
Copy after login

结果如下:

2.png

2、多个字段去重

对多个字段去重,表示选取多个字段拼接的一条记录,不重复的所有记录

示例表: psur_list

3.png

PLAN_NUMBER和PRODUCT_NAME字段去重,语句:

SELECT DISTINCT PLAN_NUMBER,PRODUCT_NAME FROM psur_list;
Copy after login

结果如下:

4.png

期望结果:只对第一个参数PLAN_NUMBER取唯一值

解决办法一: 使用 group_concat 函数

语句:SELECT GROUP_CONCAT(DISTINCT PLAN_NUMBER) AS PLAN_NUMBER,PRODUCT_NAME FROM psur_list GROUP BY PLAN_NUMBER

解决办法二:使用group by

语句:SELECT PLAN_NUMBER,PRODUCT_NAME FROM psur_list GROUP BY PLAN_NUMBER

结果如下:

5.png

更多相关知识,请访问 PHP中文网!!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between HQL and SQL in Hibernate framework? What is the difference between HQL and SQL in Hibernate framework? Apr 17, 2024 pm 02:57 PM

What is the difference between HQL and SQL in Hibernate framework?

Usage of division operation in Oracle SQL Usage of division operation in Oracle SQL Mar 10, 2024 pm 03:06 PM

Usage of division operation in Oracle SQL

What does the identity attribute in SQL mean? What does the identity attribute in SQL mean? Feb 19, 2024 am 11:24 AM

What does the identity attribute in SQL mean?

Comparison and differences of SQL syntax between Oracle and DB2 Comparison and differences of SQL syntax between Oracle and DB2 Mar 11, 2024 pm 12:09 PM

Comparison and differences of SQL syntax between Oracle and DB2

How does Java use the MySQL driver interceptor to implement SQL time-consuming calculations? How does Java use the MySQL driver interceptor to implement SQL time-consuming calculations? May 27, 2023 pm 01:10 PM

How does Java use the MySQL driver interceptor to implement SQL time-consuming calculations?

Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Feb 26, 2024 pm 07:48 PM

Detailed explanation of the Set tag function in MyBatis dynamic SQL tags

How to solve the 5120 error in SQL How to solve the 5120 error in SQL Mar 06, 2024 pm 04:33 PM

How to solve the 5120 error in SQL

How to use SQL statements for data aggregation and statistics in MySQL? How to use SQL statements for data aggregation and statistics in MySQL? Dec 17, 2023 am 08:41 AM

How to use SQL statements for data aggregation and statistics in MySQL?

See all articles