mysql SELECT语句去除某个字段的重复信息,需要的朋友可以收藏下。
SELECT语句,去除某个字段的重复信息,例如:
表名:table
id uid username message dateline
1 6 a 111 1284240714(时间戳)
2 6 a 222 1268840565
3 8 b 444 1266724527
4 9 c 555 1266723391
执行语句(去除username字段重复信息并按时间排序):
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
结果:
id uid username message dateline
1 6 a 111 1284240714(时间戳)
3 8 b 444 1266724527
4 9 c 555 1266723391
此语句用于显示最新记录信息,在一个区域内不允许某个信息(例如:用户)同时出现多次(一次以上)。
后记:效率问题
开始用了个这语句:
select * from table where dateline IN ( select max(dateline) from table GROUP BY uid ) ORDER BY dateline DESC
IN:当处理数据量比较大的时候,就没效率可言了,所以优化成内联,计算下快了6倍多。。。
继续条效率就加索引了~~
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn