-sql语法-2-

生产上比较标准的查法:

select * from ruozedata.studentinfo;                   ## 查询所有列
select num,name,age from ruozedata.studentinfo;        ## 查询指定列

排序 order by 滞后

asc升序desc降序
num后面没写默认升序
select num,name,age from ruozedata.studentinfo
where name like 'y%' order by num;        ##不跟默认升序

select num,name,age from ruozedata.studentinfo
where name like 'y%' order by num asc;    ##升序

select num,name,age from ruozedata.studentinfo
where name like 'y%' order by num desc;   ##降序

在这里插入图片描述
在这里插入图片描述

按部门编码排序且按月薪从大到小
select * from emp order by deptno ,sal desc;     ##降序,order by 后面可以跟多个字段排序

限制多少行

限制多少行  where(条件)    limit(限制) 
select * from emp;              ##生产上勿这样会拉垮服务器

select * from emp limit 2;      ##限制两行数据

select * from emp where deptno=10;            ##条件deptno=10

select * from emp where deptno=10 limit 2;      ##条件deptno=10限制两行

聚合函数与分组语法

1聚合函数 sum(薪水) count(人) avg(平均值) max(最大值) min(最小值) 
2分组语法 select xxx,sum (yyy)form t group by xxx       #group by出现的字段 务必出现在 select 后面
#group by 后面可以出现多个字段,取决于对题目的解析
#sum     count(计数) 不分 坑 
# having 过滤 等价于 子表+where

例题

#1.求员工表所有人的薪水和
select
sum(sal) as ss
from emp;

#2.求员工表的各个部门的薪水和
select
deptno,
sum(sal)as ss
from emp
group by deptno;

#3.求员工表的各个部门的薪水和、员工数、平均薪资
select
deptno,
sum(sal)as ssum,
count(ename)as pcount,
sum(sal)/count(ename)as meansal1,

avg(sal)as meansal2

from emp
group by deptno;

#4.找薪水和>9000的是哪个部门?
select
deptno,
sum(sal)as ssal

from emp
group by deptno 
having sum(sal)>9000;

#查询的结果 作为一张表  子表   ##方法二
select *
from
(select
deptno,
sum(sal)as ssal

from emp
group by deptno)as t where t.ssal>9000;

#5 各个部门各个职业薪水和

select       ##查询
deptno,job,  ##部门,职业

sum(sal) as ssum,    薪金求和
count(sal) as pcount   薪水计数,几个人
from emp               从emp表提取数据
group by deptno,job;    分组依据是部门,职业

综合语法

1 where               2 group   by
3 order by            4 limit
20号和30号部门的薪水和,限制两行

select
deptno,job,

sum(sal)as ssum,
count(sal)as pcount
from emp

where deptno !=10           ##1   
group by deptno,job         ##2
order by deptno,ssum desc   ##3
limit 2;                    ##4

join语法 多表

(那张表数据全,以那张表为主)

left join 以左表为主

以左表为主, a<--b,a数据最全  b是匹配  匹配多少算多少 on就是匹配条件

select   查询
a.*,     a表所有
b.*      b表所有


from testa as a    从testa 作为 a
left join testb as b on a.aid=b.bid     左连接    testb 作为 b   在  a.aid关联b.bid

where b.bid is not null;   条件是 b.bid  不为空

right join 以右表为主

以右表为主   a-->b b数据最全  a是匹配  匹配多少算多少 on就是匹配条件

select   查询
a.*,     a表所有
b.*      b表所有


from testa as a    从testa 作为 a
right join testb as b on a.aid=b.bid     右连接    testb 作为 b   在  a.aid关联b.bid

where a.aid is not null;   条件是 b.bid  不为空

在这里插入图片描述

inner join a.b两张表的数据都存在的

select   
a.*,    
b.*     

from testa as a    
inner join testb as b on a.aid=b.bid 

在这里插入图片描述

union all 结果不去重复

union去重复 注意:数量相同 类型相同

full join     spark sql 支持的  但是我们现在学的MySQL 不支持 ,
就用 左连接+ union+右连接 

#select 
#a.*,
#b.*
#from testa as a 
#full join testb as b on a.aid=b.bid;     ##此语法MySQL不支持

查询两个表所有的数据

select   ##查询
a.*,     ##a表所有
b.*      ##b表所有
from     ##从 
testa as a     ##  testa作为a
left join testb as b  on a.aid=b.bid   左连接 testb作为b  作为  a.aid关联b.bid
union   ##合并     两个结果集相同的它只保留一行
select    ##查询
a.*,     ##a表所有
b.*      ##b表所有
from     ##从
testa as a      ##  testa作为a
right join testb as b  on a.aid=b.bid   右连接 testb作为b  作为  a.aid关联b.bid

若案列(数据量的比对)则需:最终是a表是5条,b表是5null则进行增补数据;且7-10多了进行delete删数据。
在这里插入图片描述

select  aid from testa
union all              ##不去重
select  bid from testb;
select  aid from testa
union                   ##去重
select  bid from testb;
                        ## 换表名查询出的数据两者一样,但注意 id名称会发生变化,以前面的为主
select  bid from testb
union                   ##去重
select  aid from testa;

top 1

题目:找到各部门薪金和,top1的职业?

1 每个职业的薪水和,放置创建视图sal薪金

在这里插入图片描述

select                   查询
deptno,job,    部门,职业
sum(sal+ifnull(comm,0))        求和(薪金+佣金)    ##因为null+值都为null ,所以(薪金+如果为空(佣金,0))
from  emp         从emp表中
group by  deptno,job;     分组为部门,职业

在这里插入图片描述

drop view sal;                       ##放置视图  sal

create view  sal    ##创建视图薪金  sal
as                        ## 作为

select               ##查询
deptno,job,     ##部门,职业
sum(sal+ifnull(comm,0)) as sal       ##求和(薪金+佣金)作为薪金
from  emp                                      ## 从emp表中
group by  deptno,job;                  ##分组为部门,职业

select * from sal;             查询   薪金

在这里插入图片描述

2.通过上面算出各部门的职业的薪水和,进一步算top1的职业?

select     查询
a.*        (a是做的别名)a表所有字段
from sal a    从 薪金 a
where      条件
(
select count(*) from sal b   ##查询   计数(结果集的总条数) 从薪金 b   为0的数据是top1职业
where a.deptno=b.deptno   条件 a表的部门编号=b表的部门编号
and a.sal<b.sal    和 a表的薪水小于b表的薪水       ## ##count 此处原理是:同部门 b表薪水大于a表薪水的count
) =0                   =0                                         
order by a.deptno;     排序依据  a表部门编号  不跟默认升序

##上面语句出现的a.b表是,通过第一步创建的视图sal薪金,一个表设置别名为a,一个表设置别名为b。不同的表,但内容一样,有一种说法叫作表自己union关联自己。

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值