MySQL的Distinct:去重

SELECT * FROM `name`;

-- 查詢出三條  去除了重複的
select distinct name from name;

-- 會查詢出五條  其實是distinct(name,id)
select distinct name,id from name;

-- 報錯  distinct必須放在頭部
select id,DISTINCT name from name;


如果要查詢不重複的記錄,有時候也可以用group by :
select id,name from user group by name;
distinct有些浪費資源,使用group by比distinct都要好!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章