search
Homephp教程PHP开发Usage of distinct in SQL (analysis of four examples)

When using mysql, sometimes you need to query for unique records in a certain field. Although mysql provides the distinct keyword to filter out redundant duplicate records and keep only one, it is often only used to return the number of unique records. , instead of using it to return all values ​​without duplicate records. The reason is that distinct can only return its target field and cannot return other fields. This problem has troubled me for a long time. If it cannot be solved with distinct, I can only solve it with a double loop query, which is very difficult for a large amount of data. For the website, it will undoubtedly directly affect the efficiency, so I wasted a lot of time.

In the table, there may be duplicate values. This is not a problem, but sometimes you may want to just list distinct values. The keyword distinct is used to return uniquely distinct values.

Table A:

Usage of distinct in SQL (analysis of four examples)

Example 1

select distinct name from A

The results after execution are as follows:

Usage of distinct in SQL (analysis of four examples)

Example 2

select distinct name, id from A

The results after execution are as follows:

Usage of distinct in SQL (analysis of four examples)

In fact, it is based on "name+id" To remove duplicates, distinct acts on both name and id. This method is supported by Access and SQL Server at the same time.

Example 3: Statistics

select count(distinct name) from A;   --表中name去重后的数目, SQL Server支持,而Access不支持
select count(distinct name, id) from A;   --SQL Server和Access都不支持

Example 4

select id, distinct name from A;   --会提示错误,因为distinct必须放在开头

Others

The fields displayed in the select statement can only be the fields specified by distinct, and other fields are not possible. For example, if table A has a "Remarks" column, if you want to obtain the distinct name and the corresponding "Remarks" field, it is impossible to do so directly through distinct.


Statement
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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)