SQL distinct usage

Source: Internet
Author: User
Tags table name

SQL SELECT DISTINCT statement
Duplicate values may exist in the table. This is not a problem. However, sometimes you may want to list only different values.

The keyword DISTINCT is used to return a unique value.

Syntax:
Select distinct column name FROM table name

To SELECT only different values from the Company column, use the select distinct statement:

The code is as follows: Copy code

Select distinct Company FROM Orders


How to disable distinct keywords


The distinct keyword can only be used to filter all records in the query field that are the same (the record set is the same).

Distinct keywords are sorted, which is very inefficient.

Select distinct name from t1 can eliminate duplicate records, but only one field can be taken. Now, the value of id and name must be taken at the same time.
Select distinct id, name from t1 can take multiple fields, but only records with the same values of the two fields can be eliminated.
Therefore, you cannot use distinct to achieve the desired effect. You can use group by to solve this problem.

For example, the fields to be displayed are A, B, and C, and the content of field A cannot be repeated. You can use the following statement:

The code is as follows: Copy code
Select A, min (B), min (C), count (*) from [table] where [condition] group by
Having [condition] order by A desc

For better display of the title header, replace select A, min (B), min (C), count (*) with select A as A, min (B) as B, min (C)

C, count (*) as repeated times

The displayed fields and sorting fields must be included in group.
However, the displayed fields include min, max, count, avg, sum, and other aggregate functions.
Min (B), min (C), count (*)
The general condition is written after the where clause
Conditions with aggregate functions are written after having.
If having plus count (*)> 1 in the preceding statement, we can find records with repeat times of record A greater than 1.
If having plus count (*)> 2 in the preceding statement, we can find records with repeat times of record A greater than 2.
If having plus count (*)> = 1 in the preceding statement, all records can be queried, but only one record is displayed, and the number of repeat times is displayed later-

--- This is the expected result, and the statement can be passed through hibernate

The following statement can query the data that is duplicated:
Select Field 1, field 2, count (*) from table name group by field 1, field 2 having count (*)> 1

Change the ">" number above to "=" to query the non-duplicated data.

For example

The code is as follows: Copy code

Select count (*) from (select gcmc, gkrq, count (*) from gczbxx_zhao t group by gcmc, gkrq having

Count (*)> = 1 order by GKRQ)

Recommended:

Select * from gczbxx_zhao where viewid in (select max (viewid) from gczbxx_zhao group

Gcmc) order by gkrq desc

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.