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去重

评论:

marinersdiary
2019-02-09 02:15
<a href="http://www.sguerradesign.com/adidas-originals-camo-hat-hath">adidas originals camo hat</a> <a href="http://www.zeneventsja.com/nike-roshe-run-tout-gris-chaussuresh">nike roshe run tout gris</a> <a href="http://www.adharaweb.com/nike-zoom-winflo-3-violet-gris-chaussuresj">nike zoom winflo 3 violet gris</a> <a href="http://www.jrelselaw.com/nike-air-pegasus-92-gr%C3%BCn-blau-schuhej">nike air pegasus 92 gr眉n blau</a> <a href="http://www.intellalegal.com/nike-kyrie-3-bl%C3%A5-guld-skok">nike kyrie 3 bl氓 guld</a> <a href="http://www.carmelscoffee.com/femminile-nike-air-force-1-nero-gituttio-scarpeh">femminile nike air force 1 nero gituttio</a>
marinersdiary http://www.marinersdiary.com/
delmaroffice
2019-02-05 11:18
<a href="http://www.eworklife.com/jordan-woolly-hats-sale-hatb">jordan woolly hats sale</a> <a href="http://www.fullcountkbo.com/air-jordan-future-high-customized-runninge">air jordan future high customized</a> <a href="http://www.shackofstones.com/nike-air-force-1-mennns-gr%C3%A5-schuhea">nike air force 1 mennns gr氓</a> <a href="http://www.tamworthbacon.com/herren-nike-air-rejuven8-mule-rot-orange-scarpeb">herren nike air rejuven8 mule rot orange</a> <a href="http://www.dvmengineers.com/womens-nike-huarache-utility-black-denmark-shoeso">womens nike huarache utility black denmark</a> <a href="http://www.questoffine.com/nike-free-3.0-v4-bl%C3%A5-gray-kvinders-l%C3%B8b-sko-online-kurpesc">nike free 3.0 v4 bl氓 gray kvinders l酶b sko online</a>
delmaroffice http://www.delmaroffice.com/
pistonari
2019-01-26 03:43
<a href="http://www.kainjipark.com/custom-painted-converse-uk-shoesz">custom painted converse uk</a> <a href="http://www.lapthelakes.com/nike-air-max-2011-marr%C3%B3n-gris-schuheb">nike air max 2011 marr贸n gris</a> <a href="http://www.delmaroffice.com/diamondbacks-4-ketel-marte-camo-realtree-collection-cool-base-stitched-mlb-jersey-nflu">diamondbacks 4 ketel marte camo realtree collection cool base stitched mlb jersey</a> <a href="http://www.medieneinsatz.com/nike-tiempo-legend-womens-skou">nike tiempo legend womens</a> <a href="http://www.linexcanton.com/nike-huarache-safari-hierarchy-runningf">nike huarache safari hierarchy</a> <a href="http://www.saskostomy.com/uk-trainers-adidas-stan-smith-one-piece-strap-vapor-pink-zapatosu">uk trainers adidas stan smith one piece strap vapor pink</a>
pistonari http://www.pistonari.com/
ray ban rb2140 902
2019-01-25 18:53
<a href="http://www.roozlink.com/kids-notre-dame-fighting-irish-customized-navy-blue-jersey-nfli">kids notre dame fighting irish customized navy blue jersey</a> <a href="http://www.todayyoudie.com/nike-air-zoom-pegasus-31-dark-azul-corriendo-zapatos-zapatosh">nike air zoom pegasus 31 dark azul corriendo zapatos</a> <a href="http://www.kxconcrete.com/adidas-projoator-in-zapatosk">adidas projoator in</a> <a href="http://www.mpresscards.com/north-face-pullover-ski-jacket-northfaceh">north face pullover ski jacket</a> <a href="http://www.flexinfonet.com/nike-blazer-mid-retro-wei%C3%9F-schuhek">nike blazer mid retro wei脽</a> <a href="http://www.followmellow.com/adidas-nmd-runner-guld-gr%C3%A5-skoi">adidas nmd runner guld gr氓</a>
ray ban rb2140 902 http://www.cashfordick.com/ray-ban-rb2140-902-sunglassesh
nike open cap
2019-01-16 14:40
<a href="http://www.akyakurdele.com/ralph-lauren-leder-wei%C3%9F-canvas-large-handbag-polor">ralph lauren leder wei脽 canvas large handbag</a> <a href="http://www.biggamebets.com/cheap-nike-free-4.0-flyknit-womens-purple-runninge">cheap nike free 4.0 flyknit womens purple</a> <a href="http://www.ptitbonhomme.com/prada-wallet-men-pradar">prada wallet men</a> <a href="http://www.piotrpawlik.com/air-jordan-after-game-i-red-purple-shoesa">air jordan after game i red purple</a> <a href="http://www.krabbetor.com/adidas-pr%C3%B8dator-mania-new-kurpesa">adidas pr酶dator mania new</a> <a href="http://www.carroofbags.com/nike-free-3.0-urban-orange-kit-sportsd">nike free 3.0 urban orange kit</a>
nike open cap http://www.undiabueno.com/nike-open-cap-hats
taskanizerapp
2019-01-16 03:21
<a href="http://www.aloulavideos.com/hombres-air-max-90-em-todas-gris-schuheb">hombres air max 90 em todas gris</a> <a href="http://www.brutalistmap.com/adidas-ultra-boost-core-schwarz-vgf-chaussuresd">adidas ultra boost core schwarz vgf</a> <a href="http://www.glassurchin.com/flyknit-trainer-squadron-bl%C3%A5-lyser%C3%B8d-teal-butyf">flyknit trainer squadron bl氓 lyser酶d teal</a> <a href="http://www.hevikauppa.com/jordan-retro-6-hvit-rosa-scarpea">jordan retro 6 hvit rosa</a> <a href="http://www.indegrow.com/adidas-originals-zx-700-w-sort-leopard-schuheb">adidas originals zx 700 w sort leopard</a> <a href="http://www.motiongfxart.com/nike-zoom-pegasus-33-femminile-grigio-argento-butye">nike zoom pegasus 33 femminile grigio argento</a>
taskanizerapp http://www.taskanizerapp.com/
theprintjobs
2019-01-10 00:29
<a href="http://www.agichipay.com/nike-air-max-thea-herren-marine-blau-dark-blau-wei%C3%9F-trainers-schuhea">nike air max thea herren marine blau dark blau wei脽 trainers</a> <a href="http://www.millsbulb.com/bon-march%C3%A9-nike-air-max-90-junior-scarpeb">bon march茅 nike air max 90 junior</a> <a href="http://www.hamshrd.com/womens-nike-air-max-bw-white-netherlands-shoeso">womens nike air max bw white netherlands</a> <a href="http://www.lroyroofing.com/nike-roshe-run-scarpe-da-ginnastica-kurpesb">nike roshe run scarpe da ginnastica</a> <a href="http://www.golfbodyshop.com/nike-air-max-2011-white-black-stealth-zapatost">nike air max 2011 white black stealth</a> <a href="http://www.yufnsyb.com/white-leather-converse-shoesz">white leather converse</a>
theprintjobs http://www.theprintjobs.com/
altharmony
2019-01-04 07:58
<a href="http://www.lauraundleon.com/billig-nike-flex-herren-rot-skob">billig nike flex herren rot</a> <a href="http://www.michaelphenow.com/boston-red-sox-47-brand-navy-franchise-fitted-hat-inserts-hata">boston red sox 47 brand navy franchise fitted hat inserts</a> <a href="http://www.kguides.com/kobe-9-low-blue-and-yellow-flag-runninge">kobe 9 low blue and yellow flag</a> <a href="http://www.nvusforex.com/nike-air-jordan-1-chaussures-noir-varsity-rouge-schuhea">nike air jordan 1 chaussures noir varsity rouge</a> <a href="http://www.dinerwaresb.com/nike-flyknit-racer-schwarz-wei%C3%9F-no-volt-scarpeb">nike flyknit racer schwarz wei脽 no volt</a> <a href="http://www.slipoverfx.com/womens-nike-air-max-bw-white-netherlands-shoeso">womens nike air max bw white netherlands</a>
altharmony http://www.altharmony.com/
2o14lqiq
2019-01-02 02:50
<a href="http://www.suopbhi0.com/air-jordan-9-blanc-and-or-ring-kurpesb">air jordan 9 blanc and or ring</a> <a href="http://www.rpus144x.com/nike-zoom-vomero-kvinders-bl%C3%A5-himmelbl%C3%A5-schuhee">nike zoom vomero kvinders bl氓 himmelbl氓</a> <a href="http://www.064eaffu.com/nike-kobe-11-flyknit-nike-bleu-butyd">nike kobe 11 flyknit nike bleu</a> <a href="http://www.2g9s16lv.com/adidas-climacool-ride-vi-rosso-nero-obuvc">adidas climacool ride vi rosso nero</a> <a href="http://www.waxhq23s.com/femmes-asics-gel-hyper-33-blanc-vert-scarpee">femmes asics gel hyper 33 blanc vert</a> <a href="http://www.2pdyrpwy.com/femmes-nike-air-max-95-orange-or-obuvd">femmes nike air max 95 orange or</a>
2o14lqiq http://www.2o14lqiq.com/
relsflorida
2018-12-30 19:04
<a href="http://www.georgesherry.com/under-armour-curry-one-womens-red-blue-runninge">under armour curry one womens red blue</a> <a href="http://www.jenalgroup.com/kobe-bryant-chaussures-2012-nike-schuhea">kobe bryant chaussures 2012 nike</a> <a href="http://www.badacamshotel.com/herren-nike-flyknit-lunar-2-rosa-lila-scarpeb">herren nike flyknit lunar 2 rosa lila</a> <a href="http://www.compressmyjs.com/nike-air-max-95-green-spain-shoeso">nike air max 95 green spain</a> <a href="http://www.fetison.com/nike-air-force-1-flyknit-high-gr%C3%BCn-silber-kurpesc">nike air force 1 flyknit high gr眉n silber</a> <a href="http://www.neffloot.com/orange-under-armour-football-cleats-zapatost">orange under armour football cleats</a>
[url=http://www.relsflorida.com/]relsflorida[/url]
thebatchseven
2018-12-27 11:02
<a href="http://www.calendarxs.com/nike-lunarglide-8-gr%C3%A5-kvinners-sko-schuheb">nike lunarglide 8 gr氓 kvinners sko</a> <a href="http://www.elindigo.com/free-og-14-br-blanc-jaune-chaussurese">free og 14 br blanc jaune</a> <a href="http://www.fabrykabarwy.com/nike-dunk-high-orange-gr%C3%A5-butyg">nike dunk high orange gr氓</a> <a href="http://www.fonepsi.com/nike-lebron-soldier-10-flyease-maschio-basketbtutti-scarpa-kit-scarpea">nike lebron soldier 10 flyease maschio basketbtutti scarpa kit</a> <a href="http://www.jehleflowers.com/kobe-7-gs-for-vendita-schuheb">kobe 7 gs for vendita</a> <a href="http://www.ksmartcity.com/nike-flyknit-trainer-p%C3%BArpura-kush-butyf">nike flyknit trainer p煤rpura kush</a>
thebatchseven http://www.thebatchseven.com/

发表评论:

Powered by emlog 冀ICP备13011830号-1