mysql数据聚合技术_Mysql 去重 聚合
示例數據表中的數據:
mysql> select * from talk_test;
+----+-------+--------+
| id | name ?| mobile |
+----+-------+--------+
| ?1 | xiao9 | 555555 |
| ?2 | xiao6 | 666666 |
| ?3 | xiao9 | 888888 |
| ?4 | xiao9 | 555555 |
| ?5 | xiao6 | 777777 |
+----+-------+--------+
進行單列去重后的結果:
mysql> select distinct(name) from talk_test;
+-------+
| name ?|
+-------+
| xiao9 |
| xiao6 |
+-------+
2 rows in set (0.01 sec)
mysql> select distinct(mobile) from talk_test;
+--------+
| mobile |
+--------+
| 555555 |
| 666666 |
| 888888 |
| 777777 |
+--------+
**只會保留指定的列的信息
進行多列去重后的結果:
mysql> select distinct name,mobile from talk_test;
+-------+--------+
| name ?| mobile |
+-------+--------+
| xiao9 | 555555 |
| xiao6 | 666666 |
| xiao9 | 888888 |
| xiao6 | 777777 |
+-------+--------+
**只有所有指定的列信息都相同,才會被認定為重復的信息
distinct和Group by 區別:
distinct只是將重復的行從結果中出去;
group by是按指定的列分組,一般這時在select中會用到聚合函數。
distinct是把不同的記錄顯示出來。
group by是在查詢時先把紀錄按照類別分出來再查詢。
group by 必須在查詢結果中包含一個聚集函數,而distinct不用。
聚合函數?:
AVG
MAX
MIN
SUM
COUNT
假定 Table 表有三列,?id, key,value?其中 id是主鍵,不能重復,key和value可能有重復記錄
使用distinct去重復:
select distinct key,value?from table?不能顯示主鍵。
使用group by 去重復 :
select?id,key,value from table A,?(select key,value, min(id) PID?from table group by key,value?) B?where A.id=B.PID
可以顯示主鍵
總結
以上是生活随笔為你收集整理的mysql数据聚合技术_Mysql 去重 聚合的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何在打开HTTPS开头的网址
- 下一篇: mysql100链接同时处理_php 连