当前位置:网站首页>数据库多表链接的查询方式
数据库多表链接的查询方式
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;
小小分享,希望大家能有所收获!!!
边栏推荐
- Network technology related topics
- Internet Management (Information Collection)
- Intranet information collection of Intranet penetration (5)
- MSF generate payload Encyclopedia
- 攻防世界MISC练习区(gif 掀桌子 ext3 )
- Sword finger offer 23 - print binary tree from top to bottom
- XSS之冷门事件
- Intranet information collection of Intranet penetration (3)
- Detailed explanation of network foundation routing
- 内网渗透之内网信息收集(一)
猜你喜欢

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

Intel oneapi - opening a new era of heterogeneity

Xray and burp linkage mining

《统计学》第八版贾俊平第十章方差分析知识点总结及课后习题答案

WEB漏洞-文件操作之文件包含漏洞

Harmonyos JS demo application development

List and data frame of R language experiment III

Experiment 6 inheritance and polymorphism

记一次edu,SQL注入实战

Sqqyw (indifferent dot icon system) vulnerability recurrence and 74cms vulnerability recurrence
随机推荐
Data mining - a discussion on sample imbalance in classification problems
Proceedingjoinpoint API use
攻防世界MISC练习区(gif 掀桌子 ext3 )
Detailed explanation of network foundation
Internet Management (Information Collection)
Hcip -- MPLS experiment
Hackmyvm target series (6) -videoclub
Sqqyw (indifferent dot icon system) vulnerability recurrence and 74cms vulnerability recurrence
2022华中杯数学建模思路
《统计学》第八版贾俊平第五章概率与概率分布
Interpretation of iterator related "itertools" module usage
Feature extraction and detection 14 plane object recognition
浅谈漏洞发现思路
搭建域环境(win)
网络基础之路由详解
AQS details
Which is more advantageous in short-term or long-term spot gold investment?
Detailed explanation of three ways of HTTP caching
Force deduction 152 question multiplier maximum subarray
关于超星脚本出现乱码问题