MySQL Select distinct


You can use the DISTINCT keyword to remove duplicate records from a field in a query.

grammar:

SELECT DISTINCT (column) FROM tb_name

example:

Suppose the user table has the following record:

uid username
1Admin
2John
3Jack
4Marry
5John
6Marry

SQL statement:

SELECT DISTINCT (username) FROM user

The results are as follows:

username
Admin
John
Jack
Marry
Note

Using the DISTINCT keyword to remove duplicate records has a large limitation. DISTINCT () can only contain one field and the query results will only return the field instead of the full data record (as in the example above).

You can try the following syntax:

SELECT DISTINCT (column), column1, column2, ... FROM tb_name

The query returns all the columns listed, but the query often invalidates the column's uniqueness, and column 1, column 2, ... can not precede DISTINCT (column).

The above example would return the following result :

uid username
1Admin
2John
3Jack
4Marry



Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook