常用简单函数:
去重:distinct
例如:select distinct job from empl;
数据拼接: concat
案例:select concat('我叫',name,',我的工作是',job) as '描述' from empl;
替换:如果空就用指定值代替:
select *,IFNULL(manager,'总经理') from empl;
模糊查询: ‘_’ 代替一个字符 ‘%’匹配 0~N 个字符
select * from empl where name like '夏__';
select * from empl where name like '%侯%';
排序:Order by 升序列 asc 降序 desc (默认是 asc升序)
select * from empl order by sal desc;
select * from empl where manager is not null order by sal desc,hire desc,job desc;
聚合:
总数:count(*) 如果是空就不 加入累计 例如:假如 manager 里有两个null
select concat('华夏公司一共',count(*) ,'人,不是经理的有:'
,count(manager) ) '简介' from empl;
平均值 avg() 最大 max() 最小 min() 求和 sum(sal);
无法计算的以默认用 0 代替 例如 汉字
select sum(sal) '总和',avg(sal) '平均',max(sal) '最高',min(sal) '最低' from empl;
分组查询
group by 分组字段
计算每个部门员工个数。
select job,count(*),max(sal) from empl group by job;
计算每个部门工资大于12000的员工个数。
select job,count(*),max(sal) from empl where sal>12000 group by job;
having
以前面查询的结果做条件来判断。
找出 各个部门员工工资大于12000的 并且在2人以上的部门。
select job,count(*),max(sal) from empl where sal>12000 group by job having count(*)>=2;
客官点个赞呗! (0)