mysql中过滤某个字段重复的数据

2014-6-9 liyangweb.com 李杨 MySQL

在使用sql查询中,经常需要去重,去重分两种,一种比较简单的是整条数据都重复,直接使用distinct关键字就可以搞定,如:

select distinct user_name from log

这样可以将log表中所有的user_name取出来,并不会出现重复,

但是如果要同时多查一些其他字段,distinct就不好使了,如:

select distinct user_name,add_time from log

由于log的添加时间不一样,所以查询出的记录并不完全一样,所以distinct不会过滤掉任何数据.

这样的情况我们要如何搞定呢?

 

select 
* 
from log_contacts 
where 
id in 
	(select 
	max(add_time) as add_time,user_name
	from log
	group by user_name)

 这样就得出了user_name不重复的所有数据,我用到了max,得到的是最新数据,如果是min,则取出来的就是最老的数据了~~~

标签: mysql distinct sql去重

评论:

mjchronicles
2018-12-06 02:11
<a href="http://www.slimtobody.com/pandora-safety-chain-review-pandorar">pandora safety chain review</a> <a href="http://www.lapcomit.com/adidas-superstar-vulc-adv-collegiate-burgundy-cream-white-runningc">adidas superstar vulc adv collegiate burgundy cream white</a> <a href="http://www.eaudurable.com/bon-march%C3%A9-nike-roshe-two-flyknit-hommes-blanc-runningf">bon march茅 nike roshe two flyknit hommes blanc</a> <a href="http://www.bambubaits.com/michael-kors-bedford-crossbody-black-mkr">michael kors bedford crossbody black</a> <a href="http://www.destinyrail.com/pink-blue-womens-nike-air-waffle-shoes-sportse">pink blue womens nike air waffle shoes</a> <a href="http://www.brendonrush.com/nike-limited-mens-pittsburgh-steelers-84-antonio-brown-80th-anniversary-throwback-nfl-jersey-89.99-nflv">nike limited mens pittsburgh steelers 84 antonio brown 80th anniversary throwback nfl jersey 89.99</a>
mjchronicles http://www.mjchronicles.com/

发表评论:

Powered by emlog 冀ICP备13011830号-1