当前位置:网站首页>数据库多表链接的查询方式
数据库多表链接的查询方式
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;
小小分享,希望大家能有所收获!!!
边栏推荐
- Low income from doing we media? 90% of people make mistakes in these three points
- 内网渗透之内网信息收集(四)
- Binary search tree concept
- JDBC事务、批处理以及连接池(超详细)
- JDBC看这篇就够了
- Mathematical modeling idea of 2022 central China Cup
- 《统计学》第八版贾俊平第三章课后习题及答案总结
- Realize applet payment function with applet cloud development (including source code)
- Ucos-iii learning records (11) - task management
- 记一次,修改密码逻辑漏洞实战
猜你喜欢

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

SystemVerilog discusses loop loop structure and built-in loop variable I

Hackmyvm Target Series (3) - vues

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

Record an edu, SQL injection practice

搭建域环境(win)

Captcha killer verification code identification plug-in

Intel oneapi - opening a new era of heterogeneity

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

小程序web抓包-fiddler
随机推荐
How does SQLite count the data that meets another condition under the data that has been classified once
Harmonyos JS demo application development
Hackmyvm target series (6) -videoclub
《统计学》第八版贾俊平第七章知识点总结及课后习题答案
JDBC read this article is enough
AQS details
HackMyvm靶機系列(3)-visions
Web vulnerability - File Inclusion Vulnerability of file operation
XSS unexpected event
网络层—简单的arp断网
7-6 local minimum of matrix (PTA program design)
Build domain environment (win)
Record once, modify password logic vulnerability actual combat
2022华中杯数学建模思路
WEB漏洞-文件操作之文件包含漏洞
Intranet information collection of Intranet penetration (4)
Middleware vulnerability recurrence Apache
New version of postman flows [introductory teaching chapter 01 send request]
xray与burp联动 挖掘
HackMyvm靶机系列(7)-Tron