1. 程式人生 > >SQL------聚集函數

SQL------聚集函數

lse commit 最大 例如 bsp 聚集 cas where sql

首先你要知道 where->group by->having->order by/limit ,這個就是寫sql語句時的順序

5種常用的聚集函數

  MAX    求最大

  MIN    求最小

  SUM   求和

  AVG   求平均

  COUNT 計算總行數

註意:聚集函數不支持父子關聯。例如:

Drop TABLE if exists t1;

drop table if exists t2;

create table t2(a int, b int, c int);

insert into t2 values(1,2,3);

insert into t2 values(2,2,2);

insert into t2 values(2,2,2);

insert into t2 values(3,2,2);

commit;

select b, c case when c > (select avg(t.b) from t2 ) then 1 else 0 end as d from t2 t group by c, b order by 1; --該語句正常執行結果是要報錯的。報錯是正常的,不報錯則有問題。

drop table if exists t2;

SQL------聚集函數