MySQL语法二

1、order by 排序
排序order by 滞后,asc升序(升序一般是默认的)、desc降序
order by后面可跟多个字段排序

select num,name,age from ruozedata.studentinfo 
where name like 'r%' order by num asc ;
#降序
select num,name,age from ruozedata.studentinfo 
where name like 'r%' order by num desc ;
#按部门编号排序且按月薪从大到小排序,
select * from xxx order by depton , sal desc;

2、limit 限制多少行、 where
select * from xxx,查询千万数据的表,有可能拖垮数据库。
DBeaver,默认执行200条

select * from xxx where deptno=10 limit 2;

注:如果生产环节直执行了select条件,未加where或linit,让DBA或者运维,执行下面命令:

#查看进程,并kill进程
#JDBC 确实需要拿1KW,普通写法肯定不行! 用流式写法读取数据
show processlists;
kill xxx

3、group by

1.与聚合函数 sum count avg max min组合使用
2.分组语法 select xxx,sum(yyy) from t group by xxx
3.group by出现的字段 务必出现在 select 后面
4.having 过滤 等价于 子表+where

找薪水和>9000的是哪个部门?  
#过滤 having
select
deptno,sum(sal) as saal
from emp
group by deptno
having saal >9000;

4、join 多表

在这里插入图片描述

# left join 以左表为主   a<--b a数据最全  b是匹配  匹配多少算多少 on就是匹配条件
#right join 以右表为主   a-->b b数据最全  a是匹配  匹配多少算多少 on就是匹配条件
#inner join 是将两张表的数据都存在

5、union
#union all 结果不去重复 union结果去重复 注意:数量相同 类型相同

select  bid as id from testb
union  
select  aid from testa;

6、topN

#哪些部门的哪些职业的薪水和,最高1位的职业是什么?
create view  sal
as 
select
deptno,job,
sum(sal+ifnull(comm,0)) as sal
from  emp 
group by  deptno,job;

select * from sal;

select 
a.*
from sal a 
where
(
select count(*) from sal b 
where a.deptno=b.deptno
and a.sal<b.sal
) =0
order by a.deptno;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值