5select的运用
四、select的运用
--汇总函数 max()最大值,min()最小值,avg()平均值
select max(age),min(age),avg(age) from tablename; --算出表中age的最大值,并非全部max(age)数据
select max(age),min(age),avg(age) from tablename where id!=1;
select * from tablename where max(age); --错误
--子查询 返回的是最大值
select *from tablename where age in(select max(age) from tablename);
select max(age)from tablename where age in(select max(age) from tablename);
--查询最小值到最大值之间的全部数据
select* from malestaff where age
between (select MIN(age) from malestaff) and (select MAX(age) from malestaff);
select count(*) form tablename --统计人数
--分组 根据id的不同进行分组
select * from tablename group by id;
--排序 根据id的不同进行排序,默认为升序,desc代表降序
select * from tablename order by id;
select * from tablename order by id desc;
--多种条件约束
select * from tablename
where age is not null --查询条件
group by age --分组字段 where意义等同于having
having count(*)>2 --分组条件 即相同age的人数〉2
order by count(*); --根据人数的不同,对查询数据进行升序