当前位置:网站首页>数据库多表链接的查询方式
数据库多表链接的查询方式
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;
小小分享,希望大家能有所收获!!!
边栏推荐
- 外网打点(信息收集)
- XSS unexpected event
- How does SQLite count the data that meets another condition under the data that has been classified once
- Experiment 9 input and output stream (excerpt)
- List and data frame of R language experiment III
- XSS (cross site scripting attack) for security interview
- Experiment five categories and objects
- 《统计学》第八版贾俊平第五章概率与概率分布
- Attack and defense world misc practice area (GIF lift table ext3)
- Interpretation of iterator related "itertools" module usage
猜你喜欢

HackMyvm靶机系列(4)-vulny

Web vulnerability - File Inclusion Vulnerability of file operation

记一次api接口SQL注入实战

How to earn the first pot of gold in CSDN (we are all creators)

Statistics, 8th Edition, Jia Junping, Chapter VIII, summary of knowledge points of hypothesis test and answers to exercises after class

Lintcode logo queries the two nearest saplings

Hackmyvm target series (3) -visions

《统计学》第八版贾俊平第三章课后习题及答案总结

Harmonyos JS demo application development

《统计学》第八版贾俊平第六章统计量及抽样分布知识点总结及课后习题答案
随机推荐
7-6 local minimum of matrix (PTA program design)
HackMyvm靶机系列(7)-Tron
The United States has repeatedly revealed that the yield of interest rate hiked treasury bonds continued to rise
Experiment 4 array
Load balancing ribbon of microservices
Interpretation of iterator related "itertools" module usage
《统计学》第八版贾俊平第七章知识点总结及课后习题答案
The difference between layer 3 switch and router
Library management system
Experiment 7 use of common classes
Hackmyvm target series (5) -warez
中间件漏洞复现—apache
Tencent map circle
JDBC read this article is enough
XSS unexpected event
《统计学》第八版贾俊平第十章方差分析知识点总结及课后习题答案
7-5 staircase upgrade (PTA program design)
XSS之冷门事件
How to earn the first pot of gold in CSDN (we are all creators)
图书管理系统