当前位置:网站首页>数据库多表链接的查询方式
数据库多表链接的查询方式
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;
小小分享,希望大家能有所收获!!!
边栏推荐
- The United States has repeatedly revealed that the yield of interest rate hiked treasury bonds continued to rise
- Statistics 8th Edition Jia Junping Chapter 14 summary of index knowledge points and answers to exercises after class
- Hackmyvm target series (3) -visions
- HackMyvm靶机系列(3)-visions
- HackMyvm靶机系列(1)-webmaster
- Harmonyos JS demo application development
- SQL injection
- Middleware vulnerability recurrence Apache
- MySQL interview questions (4)
- JDBC事务、批处理以及连接池(超详细)
猜你喜欢

Constants, variables, and operators of SystemVerilog usage

Hackmyvm target series (2) -warrior

Uibutton status exploration and customization

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

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

Ucos-iii learning records (11) - task management

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

Hackmyvm Target Series (3) - vues

Hackmyvm target series (4) -vulny

Middleware vulnerability recurrence Apache
随机推荐
Spot gold prices rose amid volatility, and the rise in U.S. prices is likely to become the key to the future
Only 40% of the articles are original? Here comes the modification method
HackMyvm靶机系列(2)-warrior
Build domain environment (win)
Solutions to common problems in database development such as MySQL
Uibutton status exploration and customization
Binary search tree concept
Load balancing ribbon of microservices
Intranet information collection of Intranet penetration (4)
A complete collection of papers on text recognition
The United States has repeatedly revealed that the yield of interest rate hiked treasury bonds continued to rise
How does SQLite count the data that meets another condition under the data that has been classified once
Always of SystemVerilog usage_ comb 、always_ iff
Realize applet payment function with applet cloud development (including source code)
JDBC看这篇就够了
Hackmyvm Target Series (3) - vues
Proceedingjoinpoint API use
Tencent map circle
Experiment 7 use of common classes (correction post)
Constants, variables, and operators of SystemVerilog usage