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 可以顯示主鍵