mysql 学习笔记 多表查询02

把一张表 想象成两张表,进行多表查询

举例:

  1. 查询 所有员工的 姓名 以及 其 上级姓名
select s1.stname, s2.stname from staff as s1, staff as s2 where s1.stmgr = s2.stid;
  1. 查询 员工李岩的 上级姓名
select s1.stname, s2.stname from staff as s1, staff as s2 where s1.stmgr = s2.stid and s1.stname='李岩';

以上是 利用表的多表查询
还可以使用子查询 来实现
比如:

 select staff.stname from staff where stid = (select stmgr from staff where stname='李岩');

复杂一些的表查询举例:
A:
有两张表,分别是员工信息表,与部门信息表,要求查询,各个部门工资最高的员工的信息
员工表:

mysql> select * from employee;
+----+-------+--------+-------+
| id | name  | salary | depid |
+----+-------+--------+-------+
|  1 | Joe   |  70000 |     1 |
|  2 | Tom   |  80000 |     1 |
|  3 | Mary  |  50000 |     2 |
|  4 | Tk    |  10000 |     3 |
|  5 | Inter |  20000 |     3 |
|  6 | Janet | 780000 |     3 |
|  7 | Li    |  75000 |     1 |
|  8 | Wang  |   2000 |     3 |
|  9 | Gao   |   5000 |     2 |
| 10 | ZhaoF |   1000 |     2 |
| 11 | ZhaoX |   2000 |     2 |
| 12 | Mx    |   5000 |     1 |
| 13 | Mi    |   6000 |     1 |
+----+-------+--------+-------+

部门表:

mysql> select * from department;
+----+--------+
| id | name   |
+----+--------+
|  1 | it     |
|  2 | kuaiji |
|  3 | yunwei |
+----+--------+

思考步骤:
1、从employee表里查询出每个部门的最高薪资,作为一张临时表 t

mysql> select depid , max(salary) as maxsalary from employee group by depid;
+-------+-----------+
| depid | maxsalary |
+-------+-----------+
|     1 |     80000 |
|     2 |     50000 |
|     3 |    780000 |
+-------+-----------+

2、将临时表 t 和 employee表进行内连接,并新增一列,显示employee表里每个员工所在部门对应的最高薪资

mysql> select e.id,e.name,e.salary, t.maxsalary,t.depid from (select depid, max(salary) as maxsalary from employee group by depid) as t inner join employee as e on t.depid=e.depid;
+----+-------+--------+-----------+-------+
| id | name  | salary | maxsalary | depid |
+----+-------+--------+-----------+-------+
|  1 | Joe   |  70000 |     80000 |     1 |
|  2 | Tom   |  80000 |     80000 |     1 |
|  3 | Mary  |  50000 |     50000 |     2 |
|  4 | Tk    |  10000 |    780000 |     3 |
|  5 | Inter |  20000 |    780000 |     3 |
|  6 | Janet | 780000 |    780000 |     3 |
|  7 | Li    |  75000 |     80000 |     1 |
|  8 | Wang  |   2000 |    780000 |     3 |
|  9 | Gao   |   5000 |     50000 |     2 |
| 10 | ZhaoF |   1000 |     50000 |     2 |
| 11 | ZhaoX |   2000 |     50000 |     2 |
| 12 | Mx    |   5000 |     80000 |     1 |
| 13 | Mi    |   6000 |     80000 |     1 |
+----+-------+--------+-----------+-------+

3、再用 employee表里每个员工的薪资字段salary 和 部门最高薪资字段列maxsalary进行判断,查询出相等数据,此处则查询出了每个部门最高薪资的员工有哪些,作为表 tt

mysql> select e.id,e.name,e.salary, t.maxsalary,t.depid from (select depid, max(salary) as maxsalary from employee group by depid) as t inner join employee as e on t.depid=e.depid where e.salary=t.maxsalary;
+----+-------+--------+-----------+-------+
| id | name  | salary | maxsalary | depid |
+----+-------+--------+-----------+-------+
|  2 | Tom   |  80000 |     80000 |     1 |
|  3 | Mary  |  50000 |     50000 |     2 |
|  6 | Janet | 780000 |    780000 |     3 |
+----+-------+--------+-----------+-------+

4、因为表 tt 没有部门名称,所以我们再将表 tt 和department 表进行内链接,查询部门id相等的数据,从而查询出每个员工所在的部门名称

mysql> select tt.*, d.name as departname from (select e.id,e.name,e.salary, t.maxsalary,t.depid as tepid from (select depid, max(salary) as maxsalary from employee group by depid) as t inner join employee as e on t.depid=e.depid where e.salary=t.maxsalary) as tt inner join department as d on tt.tepid=d.id order by tt.tepid, tt.id;
+----+-------+--------+-----------+-------+------------+
| id | name  | salary | maxsalary | tepid | departname |
+----+-------+--------+-----------+-------+------------+
|  2 | Tom   |  80000 |     80000 |     1 | it         |
|  3 | Mary  |  50000 |     50000 |     2 | kuaiji     |
|  6 | Janet | 780000 |    780000 |     3 | yunwei     |
+----+-------+--------+-----------+-------+------------+

B:
查询 那些 比本部门 平均工资 高的员工的信息

mysql> select ss.*, d.dname from (select s.stname, s.stid, s.stsal, s.stdepno, t.avgsal from (select stdepno, avg(stsal) as avgsal from staff group by stdepno) as t inner join staff as s on s.stdepno=t.stdepno where s.stsal > t.avgsal ) as ss inner join department as d on ss.stdepno = d.deptno;

另外一种写法:

mysql> select tt.*, d.dname from (select s.stid, s.stname,s.stsal,temp.avgsal, s.stdepno  from (select stdepno, avg(stsal) as avgsal from staff group by stdepno) as temp , staff as s where temp.stdepno=s.stdepno and s.stsal > temp.avgsal) as tt , department as d where tt.stdepno = d.deptno;

还有另一种简洁的写法:

select s1.* from staff as s1 where s1.stsal > (select avg(stsal) from staff as s2 where s2.stdepno = s1.stdepno );

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/382462.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Mac Redis安装入门教程

redis安装(mac) brew install redis 如果需要后台运行 redis 服务,使用命令 brew services start redis 如果不需要后台服务,则使用命令 redis-server /usr/local/etc/redis.conf 启动redis服务 执行以下命令 /usr/local/bin…

Shell 脚本基础学习

查询手册 菜鸟教程 for循环和seq的使用 echo "method 1" for i in seq 1 10; doecho $i; doneecho "method 2" for i in {1..10} doecho $i; doneecho "method 3" for i in seq 1 2 10; doecho $i; done进入目录创建文件重定向内容 cd Test …

mysql 学习笔记15 子查询

子查询定义&#xff1a; 单上子查询举例&#xff1a; 显示与 员工 关平 同一部门的员工&#xff0c; 但不包括关平 select * from staff where staff.stdepno (select staff.stdepno from staff where stname关平) and staff.stname<> 关平 ;多行子查询举例&#xff…

shell自学笔记

文章目录重定向数值比较逻辑操作符使用范围关于文件判断测试表达式test [] [[]] (())的区别sed教程AWK教程重定向 0表示标准输入 1表示标准输出 2表示标准错误输出 默认为标准输出重定向&#xff0c;与 1> 相同 2>&1 意思是把 标准错误输出 重定向到 标准输出. &…

ffmpeg简单使用小记

1. 使用ffmpeg 进行普通切片&#xff08;ts&#xff09;操作 .\ffmpeg.exe -i a.mp4 -y -f hls -c copy -hls_time 10 .\s.m3u82. 使用ffmpeg 对视频进行设置旋转参数为0 .\ffmpeg.exe -i a.mp4 -metadata:s:v:0 rotate0 -c copy outputfile.mp43. 使用文件对视频进行加密 .\…

python3安装教程配置配置阿里云

配置全新阿里云 Linux iz2ze0ajic0vbvwnjhw2bwz 3.10.0-693.2.2.el7.x86_64 #1 SMP Tue Sep 12 22:26:13 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux 安装依赖包 wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1rc1.tar.xz 新建一个文件夹存放python3 mkdir /u…

python 使用requests模块进行 视频文件的下载

公司项目需要下载一批视频文件&#xff0c; 格式是mp4和mkv的&#xff0c;就借助request模块进行了下载&#xff0c;前提是源服务器返回文件的大小&#xff0c;以及可以接受 请求头headers中带有Range参数 以下是下载逻辑&#xff1a; resp requests.head(urlreal_video_url)…

Git的多人协作和分支处理测试

首先配置ssh密钥 克隆项目 配置两台主机&#xff08;一台本地mac&#xff0c;一台云服务器&#xff09;通过这样的方式模拟多人开发。 创建分支 [root ~/Git_test_多人协作和冲突合并/branch_test]$ ls README.md [root ~/Git_test_多人协作和冲突合并/branch_test]$ git b…

python 碎片整理 threading模块小计

threading模块中&#xff0c; start()与run()方法的区别 threading.start() 方法是开启一个线程 threading.run() 方法则是普通的函数调用

git教程目录

git入门教程 PyCharm和git安装教程 Git的多人协作和分支处理测试

msyql 禁止远程访问

1. use mysql 2. select host , user from user; 查看用户 与 对应的host 3. 将 host 中是 %的改为 localhost&#xff0c; 酌情也可以其他用户 的host限制为localhost update user set host "localhost" where user "root" and host "%" 4. …

mysql索引回表

先索引扫描&#xff0c;再通过ID去取索引中未能提供的数据&#xff0c;即为回表。 建表 mysql> create table T( id int primary key, k int not null, name varchar(16), index (k))engineInnoDB;如果语句是 select * from T where ID500&#xff0c;即主键查询方式&am…

C++ 执行cmd命令 并获取输出

这是参考别人的 &#xff0c;具体来源忘了&#xff0c;唉&#xff0c;等想起来一定补上出处 头文件 PipeCmd.h #ifndef _PIPE_CMD_H_ #define _PIPE_CMD_H_#include <Windows.h>// 执行 cmd 命令, 并获取执行结果数据 BOOL PipeCmd(char *pszCmd, char *pszResultBuffe…

iterm2 保存阿里云登陆并防止断开连接

commando edit profiles新增一个页面 添加命令 ssh -A -p 22 -o ServerAliveInterval60 rootIP

QString中包含中文的时候, 转为char *

转载自 https://blog.csdn.net/mihang2/article/details/39026865 QString中包含中文的时候&#xff0c; 转为char * void FileEncWidget::QString2ANSI(QString text, char **pOut) {std::wstring wIn text.toStdWString();char *pcstr (char *)malloc(sizeof(char)*(2 * w…

brew安装

官网&#xff1a;http://brew.sh/ 安装软件&#xff1a;brew install 软件名&#xff0c;例&#xff1a;brew install wget搜索软件&#xff1a;brew search 软件名&#xff0c;例&#xff1a;brew search wget卸载软件&#xff1a;brew uninstall 软件名&#xff0c;例&#…

关于异步IO模型的学习

看到两篇不错的文章&#xff0c;转载了&#xff1a; https://www.cnblogs.com/fanzhidongyzby/p/4098546.html https://www.cnblogs.com/aspirant/p/9166944.html

centos 无法连接网络

最小化安装&#xff0c;没有ifconfig默认没法联网 cd /etc/sysconfig/network-scripts/ sudo vi ifcfg-en33 也有可能是其他后缀 找到ONBOOTno service network restart 然后yum install net-tools

C++实现utf8和gbk编码字符串互相转换

不同系统或者服务器之间消息传递经常遇到编码转换问题&#xff0c;这里用C实现了一个轻量的gbk和utf8互相转换&#xff0c;可跨平台使用。&#xff08;重量级的可以用libiconv库&#xff09; 在windows下用<windows.h>头文件里的函数进行多字节和宽字符转换&#xff0c;…

mysql5.7初始密码查看及密码重置

查看初始密码 grep temporary password /var/log/mysqld.logcat /root/.mysql_secret mysql密码找回 密码重置 vi /etc/my.cnf 在[mysqld]下加上 skip-grant-tables&#xff0c;如&#xff1a; [mysqld] datadir/var/lib/mysql socket/var/lib/mysql/mysql.sock skip-g…