当前位置:网站首页>MySQL winter vacation self-study 2022 11 (9)
MySQL winter vacation self-study 2022 11 (9)
2022-07-06 02:25:00 【Crane paper thousand】
Self association query
mysql Sometimes, when querying information, you need to perform association query on the table itself , That is, a table is associated with itself , Use one table as multiple tables . Note that the table must be aliased during self association .
preparation
use mydb3;
create table t_sanguo
(
eid int primary key,
ename varchar(20),
manager_id int,
foreign key (manager_id) references t_sanguo(eid) # Add self association query
);
insert into t_sanguo values(1,' Liu Xie ',null);
insert into t_sanguo values(2,' Liu bei ',1);
insert into t_sanguo values(3,' Guan yu ',2);
insert into t_sanguo values(4,' Zhang Fei ',2);
insert into t_sanguo values(5,' Cao Cao ',1);
insert into t_sanguo values(6,' Xu Chu ',5);
insert into t_sanguo values(7,' Dianwei ',5);
insert into t_sanguo values(8,' king of Wu in the Three Kingdoms Era ',1);
insert into t_sanguo values(9,' Zhou Yu ',8);
insert into t_sanguo values(10,' Lu Su ',8);Open the reverse table to the model

part1
-- Make association query
-- Query the information of each person in the three countries and his superiors , Such as : Guan yu Liu bei
select a.ename,b.ename from t_sanguo a,t_sanguo b where a.manager_id = b.eid;
select a.ename,b.ename from t_sanguo a join t_sanguo b on a.manager_id = b.eid;
The operation results are as follows

part2
-- Query all tasks and superiors , Including Liu Xie
select a.ename,b.ename from t_sanguo a left join t_sanguo b on a.manager_id = b.eid;
part3
-- Query all people , The superior , Superior such as : Zhang Fei , Liu bei , Liu Xie
select a.ename,b.ename,c.ename
from t_sanguo a
left join t_sanguo b on a.manager_id = b.eid
left join t_sanguo c on b.manager_id = c.eid;
边栏推荐
- Zero foundation self-study STM32 - Review 2 - encapsulating GPIO registers with structures
- Sword finger offer 30 Stack containing min function
- Initial understanding of pointer variables
- 剑指 Offer 29. 顺时针打印矩阵
- RDD conversion operator of spark
- HttpRunnerManager安装(三)-Linux下配置myql数据库&初始化数据
- Sword finger offer 29 Print matrix clockwise
- Keyword static
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- Bigder: I felt good about the 34/100 interview, but I didn't receive the admission
猜你喜欢

Campus second-hand transaction based on wechat applet

MySQL lethal serial question 1 -- are you familiar with MySQL transactions?

【MySQL 15】Could not increase number of max_ open_ files to more than 10000 (request: 65535)

零基础自学STM32-复习篇2——使用结构体封装GPIO寄存器

一位博士在华为的22年

3D drawing ()

【机器人库】 awesome-robotics-libraries

Publish your own toolkit notes using NPM
![[robot library] awesome robots Libraries](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[robot library] awesome robots Libraries

MySQL index
随机推荐
[community personas] exclusive interview with Ma Longwei: the wheel is not easy to use, so make it yourself!
UE4 - how to make a simple TPS role (I) - create a basic role
我把驱动换成了5.1.35,但是还是一样的错误,我现在是能连成功,但是我每做一次sql操作都会报这个
Ue4- how to make a simple TPS role (II) - realize the basic movement of the role
有没有sqlcdc监控多张表 再关联后 sink到另外一张表的案例啊?全部在 mysql中操作
Is there a case where sqlcdc monitors multiple tables and then associates them to sink to another table? All operations in MySQL
[postgraduate entrance examination English] prepare for 2023, learn list5 words
Paper notes: limit multi label learning galaxc (temporarily stored, not finished)
【MySQL 15】Could not increase number of max_ open_ files to more than 10000 (request: 65535)
729. My schedule I / offer II 106 Bipartite graph
0211 embedded C language learning
FTP server, ssh server (super brief)
Social networking website for college students based on computer graduation design PHP
SQL statement
Global and Chinese markets of screw rotor pumps 2022-2028: Research Report on technology, participants, trends, market size and share
How to check the lock information in gbase 8C database?
A basic lintcode MySQL database problem
构建库函数的雏形——参照野火的手册
大厂镜像库
[coppeliasim] 6-DOF path planning