当前位置:网站首页>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;
边栏推荐
- sql表名作为参数传递
- 【coppeliasim】6自由度路径规划
- A basic lintcode MySQL database problem
- [solution] every time idea starts, it will build project
- Template_ Quick sort_ Double pointer
- General process of machine learning training and parameter optimization (discussion)
- Building the prototype of library functions -- refer to the manual of wildfire
- Computer graduation design PHP college student human resources job recruitment network
- 更换gcc版本后,编译出现make[1]: cc: Command not found
- Get the relevant information of ID card through PHP, get the zodiac, get the constellation, get the age, and get the gender
猜你喜欢

【无标题】数据库中一条查询SQL执行的过程

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

Concept of storage engine

Use the list component to realize the drop-down list and address list

Using SA token to solve websocket handshake authentication

MySQL index

It's wrong to install PHP zbarcode extension. I don't know if any God can help me solve it. 7.3 for PHP environment

Campus second-hand transaction based on wechat applet

RDD partition rules of spark

Zero foundation self-study STM32 - Review 2 - encapsulating GPIO registers with structures
随机推荐
[Wu Enda machine learning] week5 programming assignment EX4 - neural network learning
【coppeliasim】6自由度路径规划
2022 eye health exhibition, vision rehabilitation exhibition, optometry equipment exhibition, eye care products exhibition, eye mask Exhibition
ftp上传文件时出现 550 Permission denied,不是用户权限问题
Sword finger offer 30 Stack containing min function
爬虫(9) - Scrapy框架(1) | Scrapy 异步网络爬虫框架
vs code保存时 出现两次格式化
2022 edition illustrated network pdf
Global and Chinese markets of general purpose centrifuges 2022-2028: Research Report on technology, participants, trends, market size and share
Campus second-hand transaction based on wechat applet
[postgraduate entrance examination English] prepare for 2023, learn list5 words
Crawler (9) - scrape framework (1) | scrape asynchronous web crawler framework
机器学习训练与参数优化的一般过程 (讨论)
It's wrong to install PHP zbarcode extension. I don't know if any God can help me solve it. 7.3 for PHP environment
SSM assembly
有没有sqlcdc监控多张表 再关联后 sink到另外一张表的案例啊?全部在 mysql中操作
Lecture 4 of Data Engineering Series: sample engineering of data centric AI
A basic lintcode MySQL database problem
【机器人手眼标定】eye in hand
Bigder: I felt good about the 34/100 interview, but I didn't receive the admission