mysql

Mysql

内连接、外连接

  • 创建一个学生表
create table students
(
    id int primary key auto_increment,
    name varchar(100)
);
  • 创建一个成绩表
create table scores
(
   scoreId int primary key auto_increment,
   stuId int not null,
   math double
);
  • 给 两张表插入数据
insert into students values(null, "smx");
insert into students values(null, "xyj");
insert into students values(null, "wdd");
insert into students values(null, "hhr");

insert into scores values(null, 1, 100);
insert into scores values(null, 2, 99);
insert into scores values(null, 3, 98);
insert into scores values(null, 5, 97);
image.png

image.png
  • 内连接
select * from students as stu inner join scores as s on stu.id = s.stuId; # 内连接
select * from students as stu join scores as s on stu.id = s.stuId; # 内连接
image.png
  • 左连接
    select * from students as stu left join scores as s on stu.id = s.stuId; # 左连接

    image.png

  • 右连接
    ``select * from students as stu right join scores as s on stu.id = s.stuId; # 右连接


    image.png
  • 外连接
select * from students as stu left join scores as s on stu.id = s.stuId
union
select * from students as stu right join scores as s on stu.id = s.stuId;  # mysql没有全外连接的实现,只能自己手动实现,取左连接和右连接的并集
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.数据库简介 人类在进化的过程中,创造了数字、文字、符号等来进行数据的记录,但是承受着认知能力和创造能力的提升,...
    大熊_7d48阅读 563评论 0 1
  • 实体与实体之间有3种对应关系,这些关系也需要存储下来 在开发中需要对存储的数据进行一些处理,用到内置的一些函数 视...
    为自己丶拼个未来阅读 310评论 0 0
  • 花了3天时间学习MySql,考了个二级MySql 书籍参考:高等教育出版社《全国计算机等级考试二级教程-MySQL...
    如果仲有听日阅读 1,307评论 4 4
  • MySQL:众多关系型数据库中的一种 仓库 --数据库 箱子 --表 数据库: 进入mysql 命令行: mysq...
    来着何人阅读 371评论 0 1
  • mysql数据库 知识要点: 单表查询 子查询 联表查询 事务 在进行查询之前,我们要先建好关系表,并往数据表中插...
    Jimmy_F阅读 294评论 0 0