当前位置:网站首页>数据库多表链接的查询方式
数据库多表链接的查询方式
2022-07-06 09:24:00 【飘飘~】
提示:本期用到SQL语句,MySQL数据库,Navicat数据库管理工具
目录
前言
首先,我们要知道数据库表为什么是多张表,因为设计成一张表会造成数据冗余,造成数据库空间浪费,然而我们有时候需要多张表的数据整合并且查询出来,这时候就需要通过表之间的主外键关系关联在一起进行查询。
本文将根据表的连接方式划分,非等值连接,等值连接(内连接,左外连接(左连接),右外连接(右连接),全外连接)两大部分进行讲解。
一、需要用到的表
①学生表(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`)
) 
②课程表(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):
CREATE TABLE `class` (
`classid` int(11) NOT NULL AUTO_INCREMENT,
`classname` varchar(20) DEFAULT NULL,
PRIMARY KEY (`classid`)
)
二、非等值连接
定义:
非等值连接即是笛卡尔积,是指两个关系中所有元素的任意组合。一般情况下,非等值查询是没有什么作用的,因为如果数据量过大,笛卡尔积形成的表会非常占用空间。
补充:笛卡尔积又称直积,表示XY,比如A表中的数据有m行,b表中的数据有n行,那么A和B做笛卡尔积,结果为mn行。
语法:
SELECT * from 表1 CROSS JOIN 表2;举例:(学生表和课程表)
SELECT * from student CROSS JOIN course;例如第一条就会展示赵雷的所有课程。

三、等值连接
1.等值连接查询方法
定义:
在连接条件中使用等于号运算符比较被连接的字段的值,其生成的虚拟表会展示被连接表的所有列,包括重复的列。
语法:
select * from 表1,表2 where 表1.字段=表2.字段2;举例: (学生表和班级表)
SELECT * FROM student,class WHERE student.classid=class.classid;
2.内连接(inner join)
语法:
select * from 表1 inner join 表2 on 条件;
举例: (学生表和班级表)
select * from student inner join class on student.classid=class.classid;

3.左外连接查询(left join)
语法:
select * from 表1 left join 表2 on 表1.字段=表2.字段;
举例: (学生表和班级表)
会以学生表为主表进行查询,张三没有班级也会显示出来。
select * from student LEFT join class on student.classid=class.classid; 
4.右外连接查询(right join)
语法:
select * from 表1 right join 表2 on 表1.字段=表2.字段;
举例: (学生表和班级表)
会以班级表为主表显示出来,如图四班没有人也会显示。
select * from student right join class on student.classid=class.classid;
5.全外连接查询(union)
语法:
select * from 表1 left join 表2 on 表1.字段=表2.字段
union
select * from 表1 right join 表2 on 表1.字段=表2.字段;
举例: (学生表和班级
select * from student LEFT join class on student.classid=class.classid
UNION
select * from student right join class on student.classid=class.classid;
小小分享,希望大家能有所收获!!!
边栏推荐
- 《统计学》第八版贾俊平第十一章一元线性回归知识点总结及课后习题答案
- Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)
- 《统计学》第八版贾俊平第四章总结及课后习题答案
- "Gold, silver and four" job hopping needs to be cautious. Can an article solve the interview?
- Load balancing ribbon of microservices
- JVM memory model concept
- 安全面试之XSS(跨站脚本攻击)
- 《统计学》第八版贾俊平第一章课后习题及答案总结
- Experiment 9 input and output stream (excerpt)
- [paper reproduction] cyclegan (based on pytorch framework) {unfinished}
猜你喜欢

Build domain environment (win)

Only 40% of the articles are original? Here comes the modification method

. Net6: develop modern 3D industrial software based on WPF (2)

How to turn wechat applet into uniapp
![New version of postman flows [introductory teaching chapter 01 send request]](/img/0f/a41a39093a1170cc3f62075fd76182.jpg)
New version of postman flows [introductory teaching chapter 01 send request]

Attack and defense world misc practice area (simplerar, base64stego, no matter how high your Kung Fu is, you are afraid of kitchen knives)

Hcip -- MPLS experiment

Hackmyvm target series (2) -warrior

内网渗透之内网信息收集(三)

JDBC read this article is enough
随机推荐
Experiment 4 array
Network layer - simple ARP disconnection
XSS之冷门事件
Database monitoring SQL execution
This article explains in detail how mockmvc is used in practical work
Statistics 8th Edition Jia Junping Chapter 7 Summary of knowledge points and answers to exercises after class
SQL注入
Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)
Experiment 7 use of common classes
Middleware vulnerability recurrence Apache
Experiment 8 exception handling
Statistics, 8th Edition, Jia Junping, Chapter 6 Summary of knowledge points of statistics and sampling distribution and answers to exercises after class
C language file operation
内网渗透之内网信息收集(二)
AQS details
xray與burp聯動 挖掘
A complete collection of papers on text recognition
Chain team implementation (C language)
Statistics 8th Edition Jia Junping Chapter 14 summary of index knowledge points and answers to exercises after class
攻防世界MISC练习区(SimpleRAR、base64stego、功夫再高也怕菜刀)