浅谈group by和distinct

        group by和distinct只了去重意义一样,但是group by应用范围更广泛些,如分组汇总或者从聚合函数里筛选数据等。

        譬如:统计每id数并且只显示数大于3

select id ,count(id) from table_name group by id having count(*)>3;

       distinct做不到。

       另外,因distinct会导致全表扫描而group by索引建的恰当的话,会有性能上提高。

猜你喜欢

转载自bijian1013.iteye.com/blog/2038268