close

mysql 可以透過distinct來過濾重複字詞,以下為一個例子

 table
 id name
 1 allen
 2 bom
 3 cry
 4 cry
 5 bom

---------------------------------------------

 

select distinct name from table
使用distinct過濾重複的name得到的結果是:

 

name
  allen
  bom
  cry

---------------------------------------------

select distinct name, id from table

將distinct放在最前面得到的結果會是:

id name
 1 allen
 2 bom
 3 cry
 4 cry
 5 bom

發現與原始資料並沒有差異,原因是因為當id與name有相同的值才會有效果

---------------------------------------------

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

取得只有名子重複的結果:

 id name count(distinct name)
 1 allen 1
 2 bom 1
 3 cry 1

PS: group by 需要放置於order by 與 limit之前

參考資料:
http://big5.china.com/gate/big5/logs.blog.china.com/200906/4842080.html
http://blog.csdn.net/qbg19881206/article/details/8648991

 

 

arrow
arrow
    文章標籤
    mysql distinct group by
    全站熱搜

    衛斯理不理 發表在 痞客邦 留言(0) 人氣()