数据分组 GROUP BY GROUP BY可以根据一个或多个字段进行分组. 比如,根据prod_id分组: SELECT prod_id ,user_id FROM products GROUP BY prod_id 注意: 1.GROUP BY可以包含多个列.比如GROUP BY prod_id,user_id ; 2.GROUP BY如果分组列中具有NULL值,则NULL会作为一个分组返回.如果列中有多行NULL,它们将分为一组; 3.GROUP BY必须出现在WHERE子句之后,ORDE
删除重复数据保留name中id最小的记录 delete from order_info where id not in (select id from (select min(id) as id from order_info group by order_number) as b); delete from table where id not in (select min(id) from table group by name having count(name)>1) and id i
删除表中重复记录,只保留一条: delete from 表名 where 字段ID in (select * from (select max(字段ID) from 表名 group by 重复的字段 having count(重复的字段) > 1) as b); 实例: 2.当想要为某一个表建立一个唯一索引,由于表中有重复记录而无法进行时,需要删除重复记录. 例表 dept id_no id_name 100 'AAA' 101 'BBB' 102 'CCC' 103 'DDD' 100 'E
结论: select xx from xx where xx group by xx order by xxx; select xx from xx group by xx having xx order by xxx; =============================== where 跟having 后面都可以跟条件.但是又有很多区别 1 where 的字段必须是表中的字段 2 执行顺序是,先where 后 聚合 3 having 后面的字段不必须是表的字段 4 having 先聚