当前位置:网站首页>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;
边栏推荐
- Minecraft 1.18.1, 1.18.2 module development 22 Sniper rifle
- Sword finger offer 29 Print matrix clockwise
- This time, thoroughly understand the deep copy
- vs code保存时 出现两次格式化
- Reset nodejs of the system
- 高数_向量代数_单位向量_向量与坐标轴的夹角
- 0211 embedded C language learning
- 2022年版图解网络PDF
- HDU_ p1237_ Simple calculator_ stack
- [Wu Enda machine learning] week5 programming assignment EX4 - neural network learning
猜你喜欢
Reset nodejs of the system
Building the prototype of library functions -- refer to the manual of wildfire
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
论文笔记: 图神经网络 GAT
[untitled] a query SQL execution process in the database
A doctor's 22 years in Huawei
Ue4- how to make a simple TPS role (II) - realize the basic movement of the role
PHP campus financial management system for computer graduation design
[depth first search notes] Abstract DFS
Minecraft 1.16.5 生化8 模组 2.0版本 故事书+更多枪械
随机推荐
3D drawing ()
Computer graduation design PHP part-time recruitment management system for College Students
Crawler (9) - scrape framework (1) | scrape asynchronous web crawler framework
Lecture 4 of Data Engineering Series: sample engineering of data centric AI
Method of changing object properties
Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
General process of machine learning training and parameter optimization (discussion)
MySQL lethal serial question 1 -- are you familiar with MySQL transactions?
PAT甲级 1033 To Fill or Not to Fill
RDD partition rules of spark
Xshell 7 Student Edition
Use the list component to realize the drop-down list and address list
Advanced technology management - what is the physical, mental and mental strength of managers
Executing two identical SQL statements in the same sqlsession will result in different total numbers
Number conclusion LC skimming review - 1
550 permission denied occurs when FTP uploads files, which is not a user permission problem
【无标题】数据库中一条查询SQL执行的过程
Building the prototype of library functions -- refer to the manual of wildfire
零基础自学STM32-复习篇2——使用结构体封装GPIO寄存器
High number_ Vector algebra_ Unit vector_ Angle between vector and coordinate axis