MySQL SELECT statement removes duplicate information for a field _mysql
Source: Internet
Author: User
SELECT statement to remove duplicate information for a field, for example:
Tables Name: Table
ID UID Username Message dateline
1 6 a 111 1284240714 (time stamp)
2 6 a 222 1268840565
3 8 B 444 1266724527
4 9 C 555 1266723391
Execute statement (remove username field repeat 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
Results:
ID UID Username Message dateline
1 6 a 111 1284240714 (time stamp)
3 8 B 444 1266724527
4 9 C 555 1266723391
Use this statement to display the most recent record information and not allow a message (for example, a user) to appear multiple times (more than once) in one area.
PostScript: Efficiency issues
I started with this statement:
SELECT * FROM table where Dateline in (select Max (Dateline) to table GROUP by UID) Order by Dateline DESC
In: When the volume of data processing is relatively large, there is no efficiency, so the optimization into an inline, calculated faster than 6 times times more ...
Continue to add efficiency on the index ~ ~
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