MySQL

######数学相关的函数
-向下取整 floor();
select floor(3.15);->3
四舍五入 round(num)
select round(23.8);->24
round(num,m)
select round(23.879);->23.88
非四舍五入
select truncate(23.879,2);->23.87
-随机数 rand();0-1
获取3,4,5随机数
select floor(rand()*3)+3
##############分组查询
group by
例如:
select avg(sal) from emp group by deptno;
#####多字段分组查询
如:
select deptno,gender,max(age) from emp group by deptno,gender;
######having
having后面可以写集合函数的条件也可以写聚合函数的条件,但是不推荐在having
后面写普通字段的条件
where 后面不能写聚合函数
having要结合分组查询使用
错误写法
select deptno,avg(sal) from emp where avg(sal)>2000 group by deptno;
正确写法:
select deptno,avg(sal) a from emp group by deptno having a>2000;
###3子查询(嵌套)可以多层嵌套
1.查询emp表中工资最高的员工信息
select max(sal) from emp;
select * from emp where sal=5000;
通过子查询把上面两条合并成一条
select * from emp where sal=(select max(sal) from emp);
子查询总结:
1.嵌套在SQL语句中的查询语句称为子查询
2.子查询可以嵌套n层
3.子查询可以嵌套在那些位置?
1.写在where或having后面作为查询的条件的值
2.写在from后面当成一张表使用 ****必须有别名###
3.写在创建表的时候
create table newemp as (select * from emp where deptno=20);

#####关联查询
同时查询多张表的查询方式称为关联查询
select e.name d.name from emp e,dept d where e.deptno=d.deptno;
#####笛卡尔积
如果关联查询不写关联关系,则得到这两张表结果的乘积,这个成绩称为笛卡尔积
-笛卡尔积是错误的查询方式导致的结果,工作中切记不要出现
######等值连接和内连接
1.等值连接:select * from A,B where A.x=B.x…;
内连接:select * from A join B on A.x=B.x where A.age=10;
#####外连接
查询A,B两张表的的数据,如果查询两张表的交集数据使用内连接或等值连接,
如果查询某一张表的全部数据两外一张表的交集数据则用外连接
左外连接:select * from A left join B on A.x=B.x where a.age=1;
右外连接:select * from A right join B on A.x=B.x where a.age=1;
查询两张表的交集用等值连接或内连接,推荐使用内连接
查询一张表的全部数据和另外一张表的交集数据使用外连接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值