Usage of DISTINCT in mysql

下次还敢
Release: 2024-04-26 04:06:13
Original
939 people have browsed it

The DISTINCT keyword is used to remove duplicate rows from MySQL query results, leaving only unique values. Its usage includes: DISTINCT column_name: Remove duplicate values from the specified column. DISTINCT(column_name1, column_name2, ...): Remove duplicate values from a combination of multiple columns.

Usage of DISTINCT in mysql

Usage of DISTINCT in MySQL

Question: What is the role of DISTINCT in MySQL?

Answer:
The DISTINCT keyword is used to remove duplicate rows from query results, retaining only unique values.

Detailed description:

DISTINCT has the following usage:

  • DISTINCT column_name:Remove duplicates from the specified column value.
  • DISTINCT(column_name1, column_name2, ...):Remove duplicate values from a combination of multiple columns.

Note:

  • DISTINCT is used to select unique rows, not to filter duplicate values. Duplicate values still exist in the result set, but are not displayed.
  • DISTINCT is sensitive to NULL values. NULL values are treated as unique values.
  • When using DISTINCT, the ORDER BY clause will affect the order of the results.
  • DISTINCT can be used in conjunction with other aggregate functions such as SUM, COUNT, AVG.
  • In some cases, using DISTINCT can reduce query performance, especially for large data sets.

Example:

SELECT DISTINCT name FROM customers;
Copy after login

This query will select all unique names from the "customers" table.

SELECT DISTINCT city, country FROM customers;
Copy after login

This query will select unique city and country combinations from the "customers" table.

The above is the detailed content of Usage of DISTINCT in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!