A, where conditions
WHERE clause of the main operators can be used in the WHERE clause, the following table:
Operators | description |
---|---|
= | equal |
<> | not equal to. Note: In some versions of SQL, the operator may be written as =! |
> | more than the |
< | Less than |
>= | greater or equal to |
<= | Less than or equal |
BETWEEN | Within a certain range |
LIKE | Search a pattern |
IN | Specify the possible values for a plurality of columns |
Two, SQL GROUP BY Syntax
SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name;
Three, SQL join query
1, left, right and inner connecting illustrated:
2, join query
1), left connection: join LEFT JOIN query to the left of the main table, for the whole; the right table is matched to the match on the show, will not match null
select a.*,b.* from a left join b as bb on a.id=b.id
Output:
2), connected to the right: the right connection to the right of the main table, full; left table are matched, the match is displayed, it is not matching nul1
select a.*,b.* from a right join b on a.id=b.id;
3), the connection: the connection, in accordance with the associated id, two tables present data show, there is no display
select a.*,b.* from a inner join b on a.id=b.id;
3、连接综合案例
案列需求1:利用聚合函数,求出年龄和
select sum(age)from rzdata; #数值求和sum(id) select count(*)from rzdata; #数值求和求条数1+1+1=3
应该注意:1、少用count(* ),应该将任一列替换*, count(0)表示第一列;
2、算age数据多少 sum(age)。count(age)表示查询age的个数,age 表示age<20的记录值,sum(age)表示age的和;
3、count(distinct id)去掉重复记录的条数,效果对比,如:
需求2:求各个名称的年龄和 sum(age)
方法一:
select sum(age)from rzdata group by name
方法二:
select * from rzdata;
select name,sum(age) from rzdata group by name;
需求3:利用子查询,注意别名,求年龄>20的,各个名称的年龄和sun(age)
方法一:
select t. name, sum(t. age) from select * from rzdata where age>20 ) as t group by t. name;
方法二:
select name,sum(age) from rzdata where age>20 group by name;
需求4:求各个名称的年龄和大于25
select name,sum(age) from rzdata group by name having sum(age)>25;
表级别名,尽量少用子查询
select * from select name, sum(age) as sumage from rzdata group by name ) as t where t. sumage >25
4、排序:
select * from ruozedata.rzdata;
select * from ruozedata.rzdata order by age desc;
按照名字排序:
select * from ruozedata.rzdata order by name desc;
#desc倒序
#asc升序 默认,可以不加该参数
select * from ruozedata.rzdata limit 2
select * from ruozedata.rzdata order by age desc limit 2
小结:
select yyyyy from zzz where xxx group by xxx having xxx order by xxx limit xxx;
上述的关键词,顺序不能颠倒
四、案例
案例需求1:查询出部门编号为30的所有员工的编号和姓名
select empno,ename from emp where deptno=30;
案例需求2:找出部门编号为10中所有经理,和部门编号为20中所有销售员的详细资料。
select * from emp where(deptno=10 and job='MANAGER') or (deptno=20 and job='SALESMAN');
案例需求3:查询所有员工详细信息,用工资降序排序,如果工资相同使用入职日期升序排序
select * from emp order by sal desc,hiredate asc;
案例需求4:列出最低薪金大于1500的各种工作及从事此工作的员工人数。 【各、每 gro by】
思路:首先查出,最低薪金大于1500的
误区1:null字段计算,比如相加nu11+任何数值=nu11
误区2:下列没有 列出最低薪金 :
最终解题:
having可以来自其他字段的数值
select job,count(job)from emp group by job having min(sal+ifnull(comm,0))>1500;
注意:
select xxx,yyyy,count(job) from emp group by xxx,yyyy;
group by后面的xxx,yyyy 应该和前面 select xxx,yyyy一致
案例需求5:列出在销售部工作的员工的姓名,假定不知道销售部的部门编号。
select ename from emp where deptno= (select deptno from dept where dname='SALES');
案例需求6:查询姓名以S开头的\以5结尾\包含S字符\第二个字母为L _
案例需求7:查询每种工作的最高工资、最低工资、人数
select max(sal+ifnull(comm,0)) as maxsal,min(sal+ifnul1(comm,0)) as minsal, count(empno) cno from emp group by job;
案例需求8:列出薪金高于公司平均薪金的所有员工号,员工姓名,所在部门名称,上级领导,工资,工资等级
select e. ename, e. deptno,d. dname, e. mgr, m.ename, e.sal+ifnull(e.comm,0) as sal, s. grade from emp e left join dept d on e. deptno=d. deptno left join emp m on e. mgr=m.empno left join salgrade s on (e.sal+ifnull(e.comm,0)) between s.losal and s.hisal where(e.sal+ifnull(e.comm,0)) > (select avg(sal+ifnull(comm,e)) from emp);
案例需求9:列出薪金 高于 在部门30工作的所有 all /任何一个 any 员工的薪金的员工姓名和薪金、部门名称。
补充:
在大数据生产上join,第一反应on的字段一定先抽样检查null条数 ,其中a left join b on a.aid=b.bid ,若bid 或者aid 有null ,笛卡尔积前,先抽样
select aid,count(aid) from a;
使用该语句进行过滤:select * from a where aid is not null