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都要好!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章