校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識(shí)庫 > mysql去重的兩種方法詳解及實(shí)例代碼

mysql去重的兩種方法詳解及實(shí)例代碼

熱門標(biāo)簽:如何選擇優(yōu)質(zhì)的外呼系統(tǒng) 桂林云電銷機(jī)器人收費(fèi) 清遠(yuǎn)申請(qǐng)400電話 沈陽智能外呼系統(tǒng)供應(yīng)商 地圖簡圖標(biāo)注 南通電銷外呼系統(tǒng)哪家強(qiáng) 手機(jī)外呼系統(tǒng)違法嗎 東莞外呼企業(yè)管理系統(tǒng) 谷歌地圖標(biāo)注位置圖解

mysql去重

方法一:    

在使用MySQL時(shí),有時(shí)需要查詢出某個(gè)字段不重復(fù)的記錄,雖然mysql提供 有distinct這個(gè)關(guān)鍵字來過濾掉多余的重復(fù)記錄只保留一條,但往往只用它來返回不重復(fù)記錄的條數(shù),而不是用它來返回不重記錄的所有值。其原因是 distinct只能返回它的目標(biāo)字段,而無法返回其它字段

下面先來看看例子:

   table
  id name
  1 a
  2 b
  3 c
  4 c
  5 b

庫結(jié)構(gòu)大概這樣,這只是一個(gè)簡單的例子,實(shí)際情況會(huì)復(fù)雜得多。

比如我想用一條語句查詢得到name不重復(fù)的所有數(shù)據(jù),那就必須使用distinct去掉多余的重復(fù)記錄。

select distinct name from table


得到的結(jié)果是:

 name
  a
  b
  c

好像達(dá)到效果了,可是,我想要得到的是id值呢?改一下查詢語句吧:

select distinct name, id from table

結(jié)果會(huì)是:

 id name
  1 a
  2 b
  3 c
  4 c
  5 b

distinct怎么沒起作用?作用是起了的,不過他同時(shí)作用了兩個(gè)字段,也就是必須得id與name都相同的才會(huì)被除。。。。。。。

我們?cè)俑母牟樵冋Z句:

select id, distinct name from table

很遺憾,除了錯(cuò)誤信息你什么也得不到,distinct必須放在開頭。難到不能把distinct放到where條件里?能報(bào)錯(cuò)。。。。。。。

最終好用的語句如下:

select *, count(distinct name) from table group by name


結(jié)果:

  id name count(distinct name)
  1 a 1
  2 b 1
  3 c 1

最后一項(xiàng)是多余的,不用管就行了,目的達(dá)到。。。。。

哦,對(duì),再順便說一句,group by 必須放在 order by 和 limit之前,不然會(huì)報(bào)錯(cuò)。。。。。。。。!OK了

總結(jié)語句:select *, count(distinct name) from (select * from table……等嵌套語句) group by name

方法二:

利用group by

SELECT * FROM( 
select * from customer where user=( 
  SELECT source_user from customer WHERE user='admin') UNION ALL select * from customer where user=( 
  select source_user from customer where user=( 
    SELECT source_user from customer WHERE user='admin')) union ALL select * from customer where user=( 
  select source_user from customer where user=( 
    select source_user from customer where user=( 
      SELECT source_user from customer WHERE user='admin'))) UNION ALL select * from customer where source_user=(/*我的上線的上線的user*/ 
  select user from customer where user=( 
    select source_user from customer where user=( 
      SELECT source_user from customer WHERE user='admin'))) union all select * from customer where source_user=(/*我的上線的上線的上線user*/ 
  select user from customer where user=( 
  select source_user from customer where user=( 
    select source_user from customer where user=( 
      SELECT source_user from customer WHERE user='admin'))))) as alias group by user; 

注意加別名,不然報(bào)錯(cuò),注意在where語句外面包裝一下,再用group by去重才會(huì)生效。

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

您可能感興趣的文章:
  • 解析mysql中:單表distinct、多表group by查詢?nèi)コ貜?fù)記錄
  • mysql SELECT語句去除某個(gè)字段的重復(fù)信息
  • MySQL 去除重復(fù)數(shù)據(jù)實(shí)例詳解
  • 一條sql語句完成MySQL去重留一
  • MySQL去重的方法整理
  • mysql 開發(fā)技巧之JOIN 更新和數(shù)據(jù)查重/去重
  • Mysql刪除重復(fù)的數(shù)據(jù) Mysql數(shù)據(jù)去重復(fù)
  • MySQL數(shù)據(jù)表合并去重的簡單實(shí)現(xiàn)方法
  • 將MySQL去重操作優(yōu)化到極致的操作方法
  • mysql自聯(lián)去重的一些筆記記錄
  • mysql優(yōu)化小技巧之去除重復(fù)項(xiàng)實(shí)現(xiàn)方法分析【百萬級(jí)數(shù)據(jù)】

標(biāo)簽:湖州 重慶 成都 內(nèi)蒙古 貴州 天津 臨沂 常德

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《mysql去重的兩種方法詳解及實(shí)例代碼》,本文關(guān)鍵詞  mysql,去,重的,兩種,方法,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《mysql去重的兩種方法詳解及實(shí)例代碼》相關(guān)的同類信息!
  • 本頁收集關(guān)于mysql去重的兩種方法詳解及實(shí)例代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 汉阴县| 两当县| 博客| 莱西市| 菏泽市| 北安市| 波密县| 曲周县| 蓬溪县| 诸城市| 冷水江市| 垫江县| 崇阳县| 修文县| 淮阳县| 富民县| 阿城市| 眉山市| 茌平县| 化隆| 贵州省| 天气| 肇东市| 白水县| 普兰店市| 岫岩| 响水县| 葫芦岛市| 安陆市| 龙川县| 大厂| 榕江县| 武山县| 托里县| 凌海市| 隆尧县| 桂东县| 舒城县| 景谷| 溧阳市| 万源市|