当前位置:网站首页>多表操作-自关联查询
多表操作-自关联查询
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;
边栏推荐
- splunk配置163邮箱告警
- [upsampling method opencv interpolation]
- NFT 交易市场主要使用 ETH 本位进行交易的局面是如何形成的?
- 高校毕业求职难?“百日千万”网络招聘活动解决你的难题
- 跨境电商是啥意思?主要是做什么的?业务模式有哪些?
- [yolov3 loss function]
- View all processes of multiple machines
- SET XACT_ ABORT ON
- Question and answer 45: application of performance probe monitoring principle node JS probe
- [singleshotmultiboxdetector (SSD, single step multi frame target detection)]
猜你喜欢
![[configuration method of win11 multi-user simultaneous login remote desktop]](/img/8f/eab81f089b236c4527a9866b2cfc25.png)
[configuration method of win11 multi-user simultaneous login remote desktop]

redis主从模式
![[yolov3 loss function]](/img/79/87bcc408758403cf3993acc015381a.png)
[yolov3 loss function]

一次生产环境redis内存占用居高不下问题排查

高校毕业求职难?“百日千万”网络招聘活动解决你的难题
![[calculation of loss in yolov3]](/img/8c/1ad99b8fc1c5490f70dc81e1e5c27e.png)
[calculation of loss in yolov3]

MySQL 巨坑:update 更新慎用影响行数做判断!!!

comsol--三维图形随便画----回转

Is it difficult to apply for a job after graduation? "Hundreds of days and tens of millions" online recruitment activities to solve your problems

yolov5目標檢測神經網絡——損失函數計算原理
随机推荐
871. Minimum Number of Refueling Stops
COMSOL--建立几何模型---二维图形的建立
Proof of the thinking of Hanoi Tower problem
如何让你的产品越贵越好卖
《增长黑客》阅读笔记
What does cross-border e-commerce mean? What do you mainly do? What are the business models?
Acid transaction theory
Solve readobjectstart: expect {or N, but found n, error found in 1 byte of
网络五连鞭
Solve the problem of slow access to foreign public static resources
Guys, I tested three threads to write to three MySQL tables at the same time. Each thread writes 100000 pieces of data respectively, using F
COMSOL -- establishment of 3D graphics
1.php的laravel创建项目
[deploy pytoch project through onnx using tensorrt]
解决readObjectStart: expect { or n, but found N, error found in #1 byte of ...||..., bigger context ..
Yolov5 target detection neural network -- calculation principle of loss function
Ziguang zhanrui's first 5g R17 IOT NTN satellite in the world has been measured on the Internet of things
pytorch-权重衰退(weight decay)和丢弃法(dropout)
NFT 交易市场主要使用 ETH 本位进行交易的局面是如何形成的?
redis主从模式

