博客
关于我
20_MySQL表的内连接实操
阅读量:459 次
发布时间:2019-03-06

本文共 1046 字,大约阅读时间需要 3 分钟。

 

 

-- 查询底薪超过公司平均底薪的员工信息-- 方法1SELECT empno,enameFROM t_empHAVING sal>AVG(sal);因为在having中不能拿一个字段与聚合函数比较,所以此种方法行不通-- 方法2SELECT e1.empno,e1.enameFROM t_emp e1 JOIN t_emp e2 ON e1.empno=e2.empnoWHERE e1.sal>AVG(e2.sal);错误:非法使用聚合函数-- 方法3SELECT e.empno,e.ename,e.salFROM t_emp e JOIN (SELECT AVG(sal) avg FROM t_emp) tON e.sal >= t.avg;一个结果也能够作为一张表与另一张表进行关联

 

 

-- 查询research部门的人数,最高底薪,最低底薪,平均底薪,平均工龄SELECT     count(*),    MAX(e.sal),    MIN(e.sal),    AVG(e.sal),    AVG(DATEDIFF(NOW(),hiredate)/365)FROM     t_emp e JOIN t_dept d ON e.deptno=d.deptnoWHERE     d.dname="RESEARCH";

 

 

-- 查询每种职业的最高工资、最低工资、平均工资、最高工资等级和最低工资等级SELECT     e.job,    MAX(e.sal+IFNULL(e.comm,0)),    MIN(e.sal+IFNULL(e.comm,0)),    AVG(e.sal+IFNULL(e.comm,0)),    MAX(s.grade),    MIN(s.grade)FROM t_emp e JOIN t_salgrade s ON (e.sal+IFNULL(e.comm,0)) BETWEEN s.losal AND s.hisalGROUP BY e.job;

 

 

 

-- 查询每个底薪超过部门平均底薪的员工信息SELECT e.deptno,e.ename,e.sal,d.avgFROM t_emp e JOIN (SELECT deptno,AVG(sal) AS avg FROM t_emp GROUP BY deptno) dON e.deptno=d.deptnoWHERE sal >= d.avgORDER BY e.deptno;

 

转载地址:http://cgkbz.baihongyu.com/

你可能感兴趣的文章
nginx+tomcat单个域名及多个域名配置
查看>>
Nginx+Tomcat实现动静分离
查看>>
nginx+Tomcat性能监控
查看>>
nginx+uwsgi+django
查看>>
nginx+vsftp搭建图片服务器
查看>>
Nginx-http-flv-module流媒体服务器搭建+模拟推流+flv.js在前端html和Vue中播放HTTP-FLV视频流
查看>>
nginx-vts + prometheus 监控nginx
查看>>
Nginx/Apache反向代理
查看>>
Nginx: 413 – Request Entity Too Large Error and Solution
查看>>
nginx: [emerg] getpwnam(“www”) failed 错误处理方法
查看>>
nginx: [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:
查看>>
nginx: [error] open() “/usr/local/nginx/logs/nginx.pid“ failed (2: No such file or directory)
查看>>
nginx:Error ./configure: error: the HTTP rewrite module requires the PCRE library
查看>>
Nginx:objs/Makefile:432: recipe for target ‘objs/src/core/ngx_murmurhash.o‘解决方法
查看>>
nginxWebUI runCmd RCE漏洞复现
查看>>
nginx_rtmp
查看>>
Vue中向js中传递参数并在js中定义对象并转换参数
查看>>
Nginx、HAProxy、LVS
查看>>
nginx一些重要配置说明
查看>>
Nginx一网打尽:动静分离、压缩、缓存、黑白名单、跨域、高可用、性能优化......
查看>>