当前位置:网站首页>多表操作-自关联查询
多表操作-自关联查询
2022-07-05 11:37:00 【汤键.TJ】
目录
基本概念
- 在同一张表中数据有关联性,我们可以把这张表当成多个表来查询
- 通过给表取不同的别名来实现
数据准备
- CREATE TABLE employee(
id INT PRIMARY KEY auto_increment, -- 员工编号
NAME VARCHAR(20), -- 员工姓名
mgr INT, -- 上级编号
salary DOUBLE -- 员工工资
); - -- 添加数据
- INSERT INTO employee VALUES (1001,'孙悟空',1005,9000.00),
- (1002,'猪八戒',1005,8000.00),
- (1003,'沙和尚',1005,8500.00),
- (1004,'小白龙',1005,7900.00),
- (1005,'唐僧',NULL,15000.00),
- (1006,'武松',1009,7600.00),
- (1007,'李逵',1009,7400.00),
- (1008,'林冲',1009,8100.00),
- (1009,'宋江',NULL,16000.00);
实例操作
- 查询所有员工的姓名及其直接上级的姓名,没有上级的员工也需要查询
- 通过mgr可以找到上级
- 然后没有上级的员工也需要查询,这就需要外连接查询
-- 查询所有员工的姓名及其直接上级的姓名,没有上级的员工也需要查询 -- 条件:employee.mgr=employee.id SELECT e1.id, e1.name, e1.mgr, e2.id, e2.name FROM employee e1 LEFT OUTER JOIN employee e2 ON e1.mgr=e2.id;
边栏推荐
- [singleshotmultiboxdetector (SSD, single step multi frame target detection)]
- Redis集群的重定向
- CDGA|数据治理不得不坚持的六个原则
- 【Win11 多用户同时登录远程桌面配置方法】
- Install esxi 6.0 interactively
- [upsampling method opencv interpolation]
- An error is reported in the process of using gbase 8C database: 80000305, host IPS long to different cluster. How to solve it?
- 技术管理进阶——什么是管理者之体力、脑力、心力
- How can China Africa diamond accessory stones be inlaid to be safe and beautiful?
- Risc-v-qemu-virt in FreeRTOS_ Scheduling opportunity of GCC
猜你喜欢
[yolov5.yaml parsing]
[configuration method of win11 multi-user simultaneous login remote desktop]
石油化工企业安全生产智能化管控系统平台建设思考和建议
COMSOL -- three-dimensional graphics random drawing -- rotation
How to make your products as expensive as possible
redis 集群模式原理
COMSOL -- establishment of 3D graphics
COMSOL -- 3D casual painting -- sweeping
How can China Africa diamond accessory stones be inlaid to be safe and beautiful?
【yolov5.yaml解析】
随机推荐
高校毕业求职难?“百日千万”网络招聘活动解决你的难题
POJ 3176 cow bowling (DP | memory search)
解决readObjectStart: expect { or n, but found N, error found in #1 byte of ...||..., bigger context ..
Implementation of array hash function in PHP
[LeetCode] Wildcard Matching 外卡匹配
阻止浏览器后退操作
阻止瀏覽器後退操作
汉诺塔问题思路的证明
15 methods in "understand series after reading" teach you to play with strings
SET XACT_ABORT ON
COMSOL -- three-dimensional graphics random drawing -- rotation
【云原生 | Kubernetes篇】Ingress案例实战(十三)
C operation XML file
Shell script file traversal STR to array string splicing
Advanced technology management - what is the physical, mental and mental strength of managers
Ncp1342 chip substitute pn8213 65W gallium nitride charger scheme
Yolov5 target detection neural network -- calculation principle of loss function
跨境电商是啥意思?主要是做什么的?业务模式有哪些?
【无标题】
技术管理进阶——什么是管理者之体力、脑力、心力