When using MySQL, it is sometimes necessary to query for a record that does not duplicate a field, although MySQL provides the DISTINCT keyword to filter out redundant duplicate records to keep only one, but it is often used only to return the number of distinct records, instead of using it to return all values that are not re-recorded. The reason is that distinct can only return its target field, and can not return other fields, this problem has plagued me for a long time, with distinct can not solve the words, I only use two-cycle query to solve, and so for a very large number of stations, will undoubtedly directly affect the efficiency of the So I wasted a lot of time.
In the table, duplicate values may be included. That's not a problem, but sometimes you might want to just list different values (distinct). Keyword DISTINCT is used to return only different values.
Table A:
Example 1
Copy CodeThe code is as follows:
Select distinct from A
The following results are performed:
Example 2
Copy CodeThe code is as follows:
Select distinct from A
The following results are performed:
In fact, according to "Name+id" to come and go, distinct at the same time on the name and ID , this way access and SQL Server support.
Example 3: Statistics
Select Count (distinct from A; -- The table name goes back to the number after SQL Server support, and Access does not support Select Count (distinct from A; -- Neither SQL Server nor access supports
Example 4
Select distinct from A; -- The error will be prompted because the distinct must be placed at the beginning
Other
The field selected in the distinct statement can only be a field specified by distinct, and other fields are not possible. For example, if Table A has a memo column, if you want to get Distinc name and the corresponding Memo field, it is not possible to go directly through distinct.
However, there are other ways to implement a discussion of how SQL Server will stitch up multiple lines of content into one row
Usage of distinct in SQL (four sample analysis)