mysql学习笔记14 多表查询初步

  1. 对数据分组的总结

在这里插入图片描述
举例:统计各个部门的平均工资,并且是大于1000的,并且按照平均工资从高到底排序

mysql> select avg(stsal) as myavgsal, stdepno from staff group by stdepno having myavgsal > 1000 order by myavgsal desc;
+-------------+---------+
| myavgsal    | stdepno |
+-------------+---------+
| 7000.200000 |      10 |
| 5350.200000 |      30 |
| 3094.766667 |      20 |
+-------------+---------+
  1. 笛卡尔积的原理
    在这里插入图片描述
    多表查询练习:
  2. 显示员工名,工资,及所在部门的名字
select staff.stname , staff.stsal, department.dname from staff, department where department.deptno = staff.stdepno;
  1. 显示部门号为10的部门名,员工名,工资,
select staff.stname , staff.stsal, department.dname from staff, department where department.deptno = staff.stdepno and department.deptno = 10;
  1. 显示每个员工的姓名,工资, 及其工资的级别
select staff.stname, staff.stsal, salalevel.slevel from staff, salalevel where staff.stsal >= slower and staff.stsal <= shig;
  1. 显示每个员工姓名,工资,以及部门名,以及工资级别
    这样就设计到三张表的关联查询,三张表为了避免笛卡尔积, 就需要至少两个查询条件
select staff.stname, staff.stsal , department.dname, salalevel.slevel from staff,department,salalevel where staff.stdepno = department.deptno and staff.stsal >= salalevel.slower and staff.stsal <= salalevel.shig;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值