– 分组查询
– 使用 group by 语句。
– 如果不想报错的话,需要将 select 后面列表中的字段添加到 group by 子句中即可。
– 其实很简单,就是以 deptno 作为一个参照物,根据参照物定义出条件对象。
select deptno, sum(sal)
from scott.emp
group by deptno;
– 如果想在分组后,还需要进行条件过滤
– 可以使用 having 关键字,追加条件
select deptno, sum(sal)
from scott.emp
group by deptno
having sum(sal) > 10000;
– 常见的关键字使用顺序:
– select > from > where > group by > having > order by