大数据学习之SQL语句二
多表关联 join
----建表
drop table testa;
create table testa(aid int,aname varchar(100));
drop table testb;
create table testb(bid int,bname varchar(100),age int);
----给表中添加数据
insert into testa values(1,'tianming');
insert into testa values(2,'si');
insert into testa values(3,'dou');
insert into testa values(4,'ben');
insert into testa values(5,'yang');
insert into testb values(1,'tianming',15);
insert into testb values(2,'si',16);
insert into testb values(3,'dou',17);
insert into testb values(4,'ben',18);
insert into testb values(7,'femh',19);
insert into testb values(8,'jepsion',20);
insert into testb values(9,'louie',21);
左连接:left join
(左表为主,其数据最全,右表匹配作表,匹配不到的用null值)
select
a.aid,b.bname,
b.bid,b.bname
from testa a left join testb b on a.aid=b.bid
---结果显示如下
右连接:right join
(右表为主,其数据最全,左表匹配作表,匹配不到的用null值)
select
a.aid,b.bname,
b.bid,b.bname
from testa a right join testb b on a.aid=b.bid
----结果如下
内链接:inner join
(两张表匹配到的数据行才显示)
select
a.aid,b.bname,
b.bid,b.bname
from testa a inner join testb b on a.aid=b.bid
---结果如下
多字段匹配
---建表
drop table testa;
create table testa(aid int,aname varchar(100));
drop table testb;
create table testb(bid int,bname varchar(100),age int);
----添加数据到表中
insert into testa values(1,'tianming1');
insert into testa values(2,'si1');
insert into testa values(3,'dou');
insert into testa values(4,'ben');
insert into testa values(5,'yang');
insert into testb values(1,'tianming2',15);
insert into testb values(2,'si2',16);
insert into testb values(3,'dou',17);
insert into testb values(4,'ben',18);
insert into testb values(7,'femh',19);
insert into testb values(8,'jepsion',20);
insert into testb values(9,'louie',21);
----根据id匹配
select
a.aid,b.bname,
b.bid,b.bname
from testa a left join testb b on a.aid=b.bid
--结果如下
----根据id和name匹配 left join 实例
select
a.aid,b.bname,
b.bid,b.bname
from testa a left join testb b on a.aid=b.bid and a.aname=b.bname;
--结果如下
----根据id和name匹配 right join 实例
select
a.aid,b.bname,
b.bid,b.bname
from testa a right join testb b on a.aid=b.bid and a.aname=b.bname;
--结果如下
----根据id和name匹配 inner join 实例
select
a.aid,b.bname,
b.bid,b.bname
from testa a inner join testb b on a.aid=b.bid and a.aname=b.bname;
--结果如下
--部门表
dept部门表(deptno部门编号/dname部门名称/loc地点)
create table dept (
deptno numeric(2),
dname varchar(14),
loc varchar(13)
);
insert into dept values (10, 'ACCOUNTING', 'NEW YORK');
insert into dept values (20, 'RESEARCH', 'DALLAS');
insert into dept values (30, 'SALES', 'CHICAGO');
insert into dept values (40, 'OPERATIONS', 'BOSTON');
--工资等级表
salgrade工资等级表(grade 等级/losal此等级的最低/hisal此等级的最高)
create table salgrade (
grade numeric,
losal numeric,
hisal numeric
);
insert into salgrade values (1, 700, 1200);
insert into salgrade values (2, 1201, 1400);
insert into salgrade values (3, 1401, 2000);
insert into salgrade values (4, 2001, 3000);
insert into salgrade values (5, 3001, 9999);
--员工表
emp员工表(empno员工号/ename员工姓名/job工作/mgr上级编号/hiredate受雇日期/sal薪金/comm佣金/deptno部门编号)
工资 = 薪金 + 佣金
create table emp (
empno numeric(4) not null,
ename varchar(10),
job varchar(9),
mgr numeric(4),
hiredate datetime,
sal numeric(7, 2),
comm numeric(7, 2),
deptno numeric(2)
);
insert into emp values (7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, null, 20);
insert into emp values (7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600, 300, 30);
insert into emp values (7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250, 500, 30);
insert into emp values (7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975, null, 20);
insert into emp values (7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250, 1400, 30);
insert into emp values (7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850, null, 30);
insert into emp values (7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450, null, 10);
insert into emp values (7788, 'SCOTT', 'ANALYST', 7566, '1982-12-09', 3000, null, 20);
insert into emp values (7839, 'KING', 'PRESIDENT', null, '1981-11-17', 5000, null, 10);
insert into emp values (7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08', 1500, 0, 30);
insert into emp values (7876, 'ADAMS', 'CLERK', 7788, '1983-01-12', 1100, null, 20);
insert into emp values (7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950, null, 30);
insert into emp values (7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000, null, 20);
insert into emp values (7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300, null, 10);
查看员工的部门坐落于哪里
select
a.ename,a.deptno,a.ename,b.loc
from emp a
left join dept b on a.deptno=b.deptno;
查看员工的所属部门是什么?在什么地方?所属的工资等级是什么?
select
a.ename,a.deptno,(a.sal+IFNULL(a.comm,0)) as sal,
b.dname,b.loc
from emp a
left join dept b on a.deptno=b.deptno
left join salgrade s on (a.sal+IFNULL(a.comm,0))between s.losal and s.hisal
---结果显示如下
注意
:
- 在做实数计算时,应先通过
IFNULL(exp,0)
函数将exp值为NUll的转化为0之后再进行计算。