首先你要知道 where->group by->having->order by/limit ,这个就是写sql语句时的顺序sql
5种经常使用的汇集函数函数
MAX 求最大spa
MIN 求最小it
SUM 求和table
AVG 求平均select
COUNT 计算总行数sql语句
注意:汇集函数不支持父子关联。例如:im
Drop TABLE if exists t1;tab
drop table if exists t2;vi
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;