Home > Database > Mysql Tutorial > body text

mysql依照某一字段去重,并显示其他字段信息

WBOY
Release: 2016-06-07 16:25:34
Original
2553 people have browsed it

mysql按照某一字段去重,并显示其他字段信息 ? 最近有一个需求按照sql中的某一列值去重,还要显示其他字段的信息, 使用select distinct name, sorce from table,结果发现去重不起作用,因为MYSQL认为要sorce和name同时重复才会去除。 ??? Google一番,反复

mysql按照某一字段去重,并显示其他字段信息

?

最近有一个需求按照sql中的某一列值去重,还要显示其他字段的信息,
使用select distinct name, sorce from table,结果发现去重不起作用,因为MYSQL认为要sorce和name同时重复才会去除。
??? Google一番,反复试验最终解决办法如下:
??? select *, count(distinct name) from table group by name
??? 如果SQL语句还有limit,order by等条件,必须放在group by后面。
这样就达到了既去重,又能够输出更多字段信息的目的.

记录之,以备忘,给大家提供帮助。

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!