当前位置:网站首页>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;
边栏推荐
猜你喜欢
Keyword static
Know MySQL database
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
The third level of C language punch in
Reset nodejs of the system
2022 eye health exhibition, vision rehabilitation exhibition, optometry equipment exhibition, eye care products exhibition, eye mask Exhibition
PAT甲级 1033 To Fill or Not to Fill
MySQL lethal serial question 1 -- are you familiar with MySQL transactions?
【社区人物志】专访马龙伟:轮子不好用,那就自己造!
Using SA token to solve websocket handshake authentication
随机推荐
Multi function event recorder of the 5th National Games of the Blue Bridge Cup
一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
SQL statement
SSM 程序集
Pat grade a 1033 to fill or not to fill
【MySQL 15】Could not increase number of max_open_files to more than 10000 (request: 65535)
2022 eye health exhibition, vision rehabilitation exhibition, optometry equipment exhibition, eye care products exhibition, eye mask Exhibition
How to generate rich text online
Use Scrollview and tabhost to realize vertical scrollbars and tabs
Paper notes: limit multi label learning galaxc (temporarily stored, not finished)
Black high-end responsive website dream weaving template (adaptive mobile terminal)
数据工程系列精讲(第四讲): Data-centric AI 之样本工程
General process of machine learning training and parameter optimization (discussion)
好用的 JS 脚本
Overview of spark RDD
Sword finger offer 29 Print matrix clockwise
[depth first search] Ji Suan Ke: Betsy's trip
【coppeliasim】6自由度路径规划
Know MySQL database
The intelligent material transmission system of the 6th National Games of the Blue Bridge Cup