当前位置:网站首页>SQL multi table query
SQL multi table query
2022-07-27 15:58:00 【Not enough to eat】
Multi-table query
basic sql sentence
SELECT < Target field > FROM < Database table > WHERE < Query criteria > GROUP BY < Group by > ORDER BY < Arrangement basis >
Multi-table query
- Internal connection : Return the data that satisfies the connection condition
Format :SELECT < Target field > FROM <INNER JOIN+ Table name +ON+ Connection condition > WHERE < Query criteria >
Be careful : Clarify the relationship between tables , Find out what data from which tables , What are the connection conditions between tables
Patients with a : Double table query

Example 2 : Multi-table query
a sys_user user_name => user_id dept_id
b sys_user_role user_id => role_id
c sys_role role_id => role_name
d sys_role_dept role_id => dept_id
e sys_dept dept_id => dept_name parent_id dept_name
f sys_dept parent_id => dept_name
SELECT
a.user_name,
a.user_id,
a.dept_id,
b.role_id,
c.role_name,
e.dept_name,
f.dept_name
FROM
sys_user AS a
INNER JOIN sys_user_role AS b ON b.user_id = a.user_id
INNER JOIN sys_role AS c ON c.role_id = b.role_id
INNER JOIN sys_dept AS e ON e.dept_id = a.dept_id
INNER JOIN sys_dept AS f ON f.dept_id = e.parent_id
WHERE
a.user_name = 'zjzsqxyf01'
External connection : The external connection lists not only the rows that match the connection conditions , Instead, list the left table ( Left outer connection )、 Right table ( Right outer connection ) Or two tables ( When fully externally connected ) All data rows that meet the search criteria in the
(1) Left connection : Return all data in the left table
Format :SELECT < Target field > FROM <LEFT JOIN+ Table name +ON+ Connection condition > WHERE < Query criteria >

(2) The right connection : Return all data in the right table
Format :SELECT < Target field > FROM <RIGHT JOIN+ Table name +ON+ Connection condition > WHERE < Query criteria >

(3) Full connection : On the basis of equivalent connection, add the unmatched data of the left table and the right table
Format :SELECT < Target field > FROM <FULL JOIN+ Table name +ON+ Connection condition > WHERE < Query criteria >

- Natural join : Based on all columns with the same fields in two tables , Return the data that meets the query conditions in the two tables
边栏推荐
- 三星关闭在中国最后一家手机工厂
- 表格插入行内公式后,单元格失去焦点
- 【云享读书会第13期】视频文件的编码格式
- Half find
- [sword finger offer] interview question 52: the first common node of two linked lists - stack, hash table, double pointer
- JS uses for in and for of to simplify ordinary for loops
- Network principle (1) - overview of basic principles
- [正则表达式] 匹配多个字符
- DRF学习笔记(准备)
- makefile 中指定程序运行时加载的库文件路径
猜你喜欢
随机推荐
/Dev/loop1 takes up 100% of the problem
scrapy爬虫框架
Division of entity classes (VO, do, dto)
C语言:字符串函数与内存函数
43亿欧元现金收购欧司朗宣告失败!ams表示将继续收购
初识结构体
It is said that the US government will issue sales licenses to Huawei to some US enterprises!
一款功能强大的Web漏洞扫描和验证工具(Vulmap)
Causes and solutions of deadlock in threads
Multimap case
[sword finger offer] interview question 50: the first character that appears only once - hash table lookup
C language: dynamic memory function
The method of exchanging two numbers in C language
DRF学习笔记(五):视图集ViewSet
台积电的反击:指控格芯侵犯25项专利,并要求禁售!
[TensorBoard] OSError: [Errno 22] Invalid argument处理
网络原理(1)——基础原理概述
IP protocol of network layer
三星关闭在中国最后一家手机工厂
DRF学习笔记(准备)








