The MySQL SELECT statement removes duplicate information of a field.
Source: Internet
Author: User
Select statement to remove duplicate information of a field, for example:
Table Name: Table
Id uid username message Dateline
1 6 A 111 1284240714 (timestamp)
2 6 A 222 1268840565
3 8 B 444 1266724527
4 9 C 555 1266723391
Execute the statement (remove duplicate username field information and sort by Time ):
Select *
From table a inner join (select max (Dateline) as Dateline
From table group by UID) B on A. Dateline = B. Dateline
Group by ID order by A. Dateline DESC
Result:
Id uid username message Dateline
1 6 A 111 1284240714 (timestamp)
3 8 B 444 1266724527
4 9 C 555 1266723391
This statement is used to display the latest record information. A certain information (such as a user) is not allowed to appear multiple times (more than once) in a region ).
Postscript: efficiency problems
The following statement is used:
Select * from table where Dateline in (select max (Dateline) from Table group by UID) order by Dateline DESC
In: when processing a large amount of data, there is no efficiency at all. Therefore, it is optimized to inline, and the computation speed is more than 6 times faster...
After the query continues, the index is added ~~
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