大数据学习之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之后再进行计算。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大数据(Big Data)是指规模庞大、复杂度高且难以用传统数据处理方法进行捕捉、管理和处理的数据集合。它通常具有以下三个特征: 数据量大:大数据指的是数据集的规模非常庞大,远远超出了传统数据处理工具的能力范围。这些数据集可能包含数十亿甚至数万亿的记录。 复杂度高:大数据往往包含多种类型和格式的数据,例如结构化数据(如关系型数据库中的数据)、半结构化数据(如XML文件)和非结构化数据(如文本、图像和音频等)。这些数据的复杂性使得处理和分析变得更加困难。 处理速度快:大数据处理要求在短时间内处理和分析大规模的数据。传统的数据处理方法往往无法满足实时或近实时处理的需求。 大数据的出现主要是由于以下几个因素的影响: 数据的爆发性增长:随着互联网的普及和各种传感器、设备的广泛应用,数据的产生和积累呈现爆发式增长的趋势。 新型数据源的涌现:除了传统的结构化数据,越来越多的非结构化和半结构化数据源涌现,例如社交媒体数据、日志文件、传感器数据、地理位置数据等。 技术的进步:大数据处理的技术工具和技术方法得到了快速发展,例如分布式计算、云计算、并行处理、机器学习和人工智能等技术的应用,使得大数据的存储、管理和分析变得可行和高效。 大数据的处理和分析可以带来许多潜在的好处,包括更深入的洞察力、更准确的决策支持、更精细的个性化服务、更高效的资源利用和创新等。在各个领域,如商业、医疗、金融、交通、科学研究等,大数据正发挥着重要的作用,并为我们带来了新的机遇和挑战。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值