当前位置:网站首页>Query method of database multi table link
Query method of database multi table link
2022-07-06 14:37:00 【Floating~】
Tips : Used in this period SQL sentence ,MySQL database ,Navicat Database management tools
Catalog
One 、 The tables you need to use
Two 、 Non equivalent connection
3、 ... and 、 Equivalent connection
1. Equivalent connection query method
2. Internal connection (inner join)
3. Left outer connection query (left join)
4. Right outer connection query (right join)
5. All external connection query (union)
Preface
First , We need to know why database tables are multiple tables , Because designing a table will cause data redundancy , Cause a waste of database space , However, sometimes we need to integrate data from multiple tables and query them , At this time, you need to query by associating the primary and foreign key relationships between tables .
This article will divide according to the connection mode of the table , Non equivalent connection , Equivalent connection ( Internal connection , The left outer join ( Left connection ), Right connection ( The right connection ), Full outer join ) Two parts are explained .
One 、 The tables you need to use
① Student list (student):
CREATE TABLE `student` (
`Sid` int(11) NOT NULL AUTO_INCREMENT,
`Sname` varchar(10) DEFAULT NULL,
`birthday` datetime DEFAULT NULL,
`Ssex` varchar(10) DEFAULT NULL,
`classid` int(11) DEFAULT NULL,
PRIMARY KEY (`Sid`)
) 
② The curriculum (course):
CREATE TABLE `course` (
`Cid` int(11) NOT NULL AUTO_INCREMENT,
`Cname` varchar(10) DEFAULT NULL,
`Tid` varchar(10) DEFAULT NULL,
PRIMARY KEY (`Cid`)
) 
③ Class table (class):
CREATE TABLE `class` (
`classid` int(11) NOT NULL AUTO_INCREMENT,
`classname` varchar(20) DEFAULT NULL,
PRIMARY KEY (`classid`)
)
Two 、 Non equivalent connection
Definition :
The non equivalent connection is the Cartesian product , It refers to any combination of all elements in two relationships . In general , Non equivalent queries are useless , Because if the amount of data is too large , The table formed by Cartesian product will take up a lot of space .
Add : Cartesian product is also called direct product , Express XY, such as A The data in the table are m That's ok ,b The data in the table are n That's ok , that A and B Do Cartesian product , The result is mn That's ok .
grammar :
SELECT * from surface 1 CROSS JOIN surface 2;give an example :( Student schedule and Curriculum )
SELECT * from student CROSS JOIN course;For example, the first article will show all the courses of Zhao Lei .

3、 ... and 、 Equivalent connection
1. Equivalent connection query method
Definition :
Use the equal sign operator in the connection condition to compare the values of the connected fields , The virtual table generated by it will show all the columns of the connected table , Include duplicate Columns .
grammar :
select * from surface 1, surface 2 where surface 1. Field = surface 2. Field 2;give an example : ( Student list and class list )
SELECT * FROM student,class WHERE student.classid=class.classid;
2. Internal connection (inner join)
grammar :
select * from surface 1 inner join surface 2 on Conditions ;
give an example : ( Student list and class list )
select * from student inner join class on student.classid=class.classid;

3. Left outer connection query (left join)
grammar :
select * from surface 1 left join surface 2 on surface 1. Field = surface 2. Field ;
give an example : ( Student list and class list )
The student table will be used as the main table for query , Zhang San will show up without class .
select * from student LEFT join class on student.classid=class.classid; 
4. Right outer connection query (right join)
grammar :
select * from surface 1 right join surface 2 on surface 1. Field = surface 2. Field ;
give an example : ( Student list and class list )
The class table will be displayed as the main table , As shown in the figure, no one in class 4 will also show .
select * from student right join class on student.classid=class.classid;
5. All external connection query (union)
grammar :
select * from surface 1 left join surface 2 on surface 1. Field = surface 2. Field
union
select * from surface 1 right join surface 2 on surface 1. Field = surface 2. Field ;
give an example : ( Student list and class
select * from student LEFT join class on student.classid=class.classid
UNION
select * from student right join class on student.classid=class.classid;
Little share , I hope you can get something !!!
边栏推荐
- Keil5-MDK的格式化代码工具及添加快捷方式
- JDBC transactions, batch processing, and connection pooling (super detailed)
- Solutions to common problems in database development such as MySQL
- 【指针】求解最后留下的人
- 《统计学》第八版贾俊平第十一章一元线性回归知识点总结及课后习题答案
- Overview of LNMP architecture and construction of related services
- 《统计学》第八版贾俊平第六章统计量及抽样分布知识点总结及课后习题答案
- Numpy快速上手指南
- 【指针】求字符串的长度
- 《统计学》第八版贾俊平第三章课后习题及答案总结
猜你喜欢

Statistics, 8th Edition, Jia Junping, Chapter 6 Summary of knowledge points of statistics and sampling distribution and answers to exercises after class

DVWA (5th week)

Intranet information collection of Intranet penetration (I)

数字电路基础(四) 数据分配器、数据选择器和数值比较器

Statistics 8th Edition Jia Junping Chapter 5 probability and probability distribution

关于超星脚本出现乱码问题
![[paper reproduction] cyclegan (based on pytorch framework) {unfinished}](/img/16/43d8929d1a37c1c68e959d5854e18c.jpg)
[paper reproduction] cyclegan (based on pytorch framework) {unfinished}

Ucos-iii learning records (11) - task management

Get started with Matplotlib drawing

Intranet information collection of Intranet penetration (2)
随机推荐
【指针】使用插入排序法将n个数从小到大进行排列
Detailed explanation of network foundation
List and data frame of R language experiment III
指针--剔除字符串中的所有数字
[issue 18] share a Netease go experience
《统计学》第八版贾俊平第十四章指数知识点总结及课后习题答案
SQL injection
“Hello IC World”
Internet Management (Information Collection)
Interview Essentials: what is the mysterious framework asking?
Intranet information collection of Intranet penetration (2)
浙大版《C语言程序设计实验与习题指导(第3版)》题目集
关于超星脚本出现乱码问题
Statistics 8th Edition Jia Junping Chapter IX summary of knowledge points of classified data analysis and answers to exercises after class
Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)
内网渗透之内网信息收集(四)
内网渗透之内网信息收集(二)
关于交换a和b的值的四种方法
函数:计算字符串中大写字母的个数
Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class