1、首先创建student库

create database student;

2、 在数据库中创建boy表 和 girl表,

mysql> create table boy(
-> boyId int primary key auto_increment,
-> boyName varchar(100) not null,
-> age int,
-> connectId int)
-> auto_increment = 1;
mysql> create table girl(
-> girlid int primary key auto_increment,
-> girlName varchar(100) not null,
-> age int,
-> connectId int)
-> auto_increment = 101;

① 对boy表插入部分数据

mysql> insert into boy(boyName,age) values('李易峰',30);
mysql> insert into boy(boyName,age) values('吴奇隆',40);
mysql> insert into boy(boyName,age) values('周润发',55);
mysql> insert into boy(boyName,age) values('周星驰',45);
mysql> insert into boy(boyName,age) values('刘德华',47);
mysql> insert into boy(boyName,age) values('成龙',60);
mysql> select * from boy;
+-------+---------+-----+-----------+
| boyId | boyName | age | connectId |
+-------+---------+-----+-----------+
| 1 | 李易峰 | 30 | NULL |
| 2 | 吴奇隆 | 40 | NULL |
| 3 | 周润发 | 55 | NULL |
| 4 | 周星驰 | 45 | NULL |
| 5 | 刘德华 | 47 | NULL |
| 6 | 成龙 | 60 | NULL |
+-------+---------+-----+-----------+

② 对girl表插入部分数据

mysql> insert into girl(girlName,age) values('刘亦菲',31);
mysql> insert into girl(girlName,age) values('唐嫣',35);
mysql> insert into girl(girlName,age) values('刘诗诗',38);
mysql> insert into girl(girlName,age) values('马苏',30);
mysql> insert into girl(girlName,age) values('杨幂',39);
mysql> insert into girl(girlName,age) values('赵丽颖',29);
mysql> select * from girl;
+--------+----------+-----+-----------+
| girlid | girlName | age | connectId |
+--------+----------+-----+-----------+
| 101 | 刘亦菲 | 31 | NULL |
| 102 | 唐嫣 | 35 | NULL |
| 103 | 刘诗诗 | 38 | NULL |
| 104 | 马苏 | 30 | NULL |
| 105 | 杨幂 | 39 | NULL |
| 106 | 赵丽颖 | 29 | NULL |
+--------+----------+-----+-----------+

3、修改表中的connectId,利用boy表中的boyId 连接girl表中的girlId

① boyId=2 连接 girlId = 102

mysql> update boy set connectId=102 where boyId=2;

② boyId=3 连接 girlId = 104

mysql> update boy set connectId=104 where boyId=3;

③  boyId=6 连接 girlId = 105

mysql> update boy set connectId=105 where boyId=6;

得出boy表

mysql> select * from boy;
+-------+---------+-----+-----------+
| boyId | boyName | age | connectId |
+-------+---------+-----+-----------+
| 1 | 李易峰 | 30 | NULL |
| 2 | 吴奇隆 | 40 | 102 |
| 3 | 周润发 | 55 | 104 |
| 4 | 周星驰 | 45 | NULL |
| 5 | 刘德华 | 47 | NULL |
| 6 | 成龙 | 60 | 105 |
+-------+---------+-----+-----------+

4、修改表中的connectId,利用girl表中的girlId 连接boy表中的boyId

① girlId=101 连接 boyd = 3

mysql> update girl set connectId=3 where girlId=101;

② girlId=103 连接 boyd = 4

mysql> update girl set connectId=4 where girlId=103;

③ girlId=105 连接 boyd = 6

update girl set connectId=6 where girlId=105;

得出girl表

mysql> select * from girl;
+--------+----------+-----+-----------+
| girlid | girlName | age | connectId |
+--------+----------+-----+-----------+
| 101 | 刘亦菲 | 31 | 3 |
| 102 | 唐嫣 | 35 | NULL |
| 103 | 刘诗诗 | 38 | 4 |
| 104 | 马苏 | 30 | NULL |
| 105 | 杨幂 | 39 | 6 |
| 106 | 赵丽颖 | 29 | NULL |
+--------+----------+-----+-----------+

5、连接,分为 内连接、左连接、有连接, where形式连接

① where连接,匹配的数据显示出来

  boy表连接girl表

mysql> select b.*,g.*
-> from boy b,girl g
-> where b.connectId = g.girlId;
+-------+---------+-----+-----------+--------+----------+-----+-----------+
| boyId | boyName | age | connectId | girlid | girlName | age | connectId |
+-------+---------+-----+-----------+--------+----------+-----+-----------+
| 2 | 吴奇隆 | 40 | 102 | 102 | 唐嫣 | 35 | NULL |
| 3 | 周润发 | 55 | 104 | 104 | 马苏 | 30 | NULL |
| 6 | 成龙 | 60 | 105 | 105 | 杨幂 | 39 | 6 |
+-------+---------+-----+-----------+--------+----------+-----+-----------+
3 rows in set

  girl表连接boy表

mysql> select b.*,g.*
-> from boy b,girl g
-> where g.connectId = b.boyId;
+-------+---------+-----+-----------+--------+----------+-----+-----------+
| boyId | boyName | age | connectId | girlid | girlName | age | connectId |
+-------+---------+-----+-----------+--------+----------+-----+-----------+
| 3 | 周润发 | 55 | 104 | 101 | 刘亦菲 | 31 | 3 |
| 4 | 周星驰 | 45 | NULL | 103 | 刘诗诗 | 38 | 4 |
| 6 | 成龙 | 60 | 105 | 105 | 杨幂 | 39 | 6 |
+-------+---------+-----+-----------+--------+----------+-----+-----------+

② 内连接,匹配的显示出来

  boy - > girl

mysql> select b.*,g.*
-> from boy b inner join girl g
-> on b.connectId = g.girlId;
+-------+---------+-----+-----------+--------+----------+-----+-----------+
| boyId | boyName | age | connectId | girlid | girlName | age | connectId |
+-------+---------+-----+-----------+--------+----------+-----+-----------+
| 2 | 吴奇隆 | 40 | 102 | 102 | 唐嫣 | 35 | NULL |
| 3 | 周润发 | 55 | 104 | 104 | 马苏 | 30 | NULL |
| 6 | 成龙 | 60 | 105 | 105 | 杨幂 | 39 | 6 |
+-------+---------+-----+-----------+--------+----------+-----+-----------+

  girl - > boy

mysql> select b.*,g.*
-> from girl g inner join boy b
-> on g.connectId = b.boyId;
+-------+---------+-----+-----------+--------+----------+-----+-----------+
| boyId | boyName | age | connectId | girlid | girlName | age | connectId |
+-------+---------+-----+-----------+--------+----------+-----+-----------+
| 3 | 周润发 | 55 | 104 | 101 | 刘亦菲 | 31 | 3 |
| 4 | 周星驰 | 45 | NULL | 103 | 刘诗诗 | 38 | 4 |
| 6 | 成龙 | 60 | 105 | 105 | 杨幂 | 39 | 6 |
+-------+---------+-----+-----------+--------+----------+-----+-----------+

③ 左外连接 left    join     on  ,哪个表在左边就偏向哪个表,把该表的数据全部列出来,不管匹配不匹配都显示

mysql>  select b.*,g.*
-> from boy b left join girl g
-> on b.connectId = g.girlId;
+-------+---------+-----+-----------+--------+----------+------+-----------+
| boyId | boyName | age | connectId | girlid | girlName | age | connectId |
+-------+---------+-----+-----------+--------+----------+------+-----------+
| 1 | 李易峰 | 30 | NULL | NULL | NULL | NULL | NULL |
| 2 | 吴奇隆 | 40 | 102 | 102 | 唐嫣 | 35 | NULL |
| 3 | 周润发 | 55 | 104 | 104 | 马苏 | 30 | NULL |
| 4 | 周星驰 | 45 | NULL | NULL | NULL | NULL | NULL |
| 5 | 刘德华 | 47 | NULL | NULL | NULL | NULL | NULL |
| 6 | 成龙 | 60 | 105 | 105 | 杨幂 | 39 | 6 |
+-------+---------+-----+-----------+--------+----------+------+-----------+

④ 右外连接 right   join     on  ,哪个表在右边就偏向哪个表,把该表的数据全部列出来,不管匹配不匹配都显示

mysql>  select b.*,g.*
-> from boy b right join girl g
-> on b.connectId = g.girlId;
+-------+---------+------+-----------+--------+----------+-----+-----------+
| boyId | boyName | age | connectId | girlid | girlName | age | connectId |
+-------+---------+------+-----------+--------+----------+-----+-----------+
| 2 | 吴奇隆 | 40 | 102 | 102 | 唐嫣 | 35 | NULL |
| 3 | 周润发 | 55 | 104 | 104 | 马苏 | 30 | NULL |
| 6 | 成龙 | 60 | 105 | 105 | 杨幂 | 39 | 6 |
| NULL | NULL | NULL | NULL | 101 | 刘亦菲 | 31 | 3 |
| NULL | NULL | NULL | NULL | 103 | 刘诗诗 | 38 | 4 |
| NULL | NULL | NULL | NULL | 106 | 赵丽颖 | 29 | NULL |
+-------+---------+------+-----------+--------+----------+-----+-----------+

⑤  全连接(笛卡尔积),MySQL不支持 full join,但是可以通过  union来实现连接结果集(剔除重复数据),会显示两张表的各自匹配的数据,union all 不会剔除重复数据

mysql> select b.*,g.*
-> from boy b left join girl g
-> on b.connectId = g.girlId
-> union
-> select b.*,g.*
-> from boy b right join girl g
-> on b.connectId = g.girlId;
+-------+---------+------+-----------+--------+----------+------+-----------+
| boyId | boyName | age | connectId | girlid | girlName | age | connectId |
+-------+---------+------+-----------+--------+----------+------+-----------+
| 1 | 李易峰 | 30 | NULL | NULL | NULL | NULL | NULL |
| 2 | 吴奇隆 | 40 | 102 | 102 | 唐嫣 | 35 | NULL |
| 3 | 周润发 | 55 | 104 | 104 | 马苏 | 30 | NULL |
| 4 | 周星驰 | 45 | NULL | NULL | NULL | NULL | NULL |
| 5 | 刘德华 | 47 | NULL | NULL | NULL | NULL | NULL |
| 6 | 成龙 | 60 | 105 | 105 | 杨幂 | 39 | 6 |
| NULL | NULL | NULL | NULL | 101 | 刘亦菲 | 31 | 3 |
| NULL | NULL | NULL | NULL | 103 | 刘诗诗 | 38 | 4 |
| NULL | NULL | NULL | NULL | 106 | 赵丽颖 | 29 | NULL |
+-------+---------+------+-----------+--------+----------+------+-----------+

2018年1月16日02:12:02,睡觉...

MySQL之表连接-> 内连接,左外连接,右外链接,全连接的更多相关文章

  1. Python进阶----多表查询(内连,左连,右连), 子查询(in,带比较运算符)

    Python进阶----多表查询(内连,左连,右连), 子查询(in,带比较运算符) 一丶多表查询     多表连接查询的应用场景: ​         连接是关系数据库模型的主要特点,也是区别于其他 ...

  2. SQL 各种连接:内连接,外连接(左外,右外,完全外)

    在讲述之前,假设有如下两个表EMP, DEPT, 并且他们数据如下:

  3. SSH安装篇之——SecureCRT连接(内网和外网)虚拟机中的Linux系统(Ubuntu)

    最近在学习Linux,看了网上很多SecureCRT连接本地虚拟机当中的Linux系统,很多都是需要设置Linux的配置文件,有点繁琐,所以自己就摸索了一下,把相关操作贴出来分享一下. SecureC ...

  4. SecureCRT连接(内网和外网)虚拟机中的Linux系统(Ubuntu)

    最近在学习Linux,看了网上很多SecureCRT连接本地虚拟机当中的Linux系统,很多都是需要设置Linux的配置文件,有点繁琐,所以自己就摸索了一下,把相关操作贴出来分享一下. SecureC ...

  5. django 内置server 外网不能访问, 报连接超时

    按照官网教程,以 python manage.py runserver 其访问url为 http://127.0.0.1:8000,意味着只能本机访问,而我的django app 部署在 阿里云上面 ...

  6. mysql数据库中的多表查询(内连接,外连接,子查询)

    用两个表(a_table.b_table),关联字段a_table.a_id和b_table.b_id来演示一下MySQL的内连接.外连接( 左(外)连接.右(外)连接.全(外)连接). MySQL版 ...

  7. 深入理解SQL的四种连接-左外连接、右外连接、内连接、全连接(转)

    1.内联接(典型的联接运算,使用像 =  或 <> 之类的比较运算符).包括相等联接和自然联接.     内联接使用比较运算符根据每个表共有的列的值匹配两个表中的行.例如,检索 stude ...

  8. 【转】深入理解SQL的四种连接-左外连接、右外连接、内连接、全连接

    [原文]:http://www.jb51.net/article/39432.htm 1.内联接(典型的联接运算,使用像 =  或 <> 之类的比较运算符).包括相等联接和自然联接.    ...

  9. 深入理解SQL的四种连接,左外连接,右外连接,内连接,全连接

    1.内联接(典型的联接运算,使用像 =  或 <> 之类的比较运算符).包括相等联接和自然联接.     内联接使用比较运算符根据每个表共有的列的值匹配两个表中的行.例如,检索 stude ...

  10. SQL的四种连接(左外连接、右外连接、内连接、全连接)

    1.内联接(典型的联接运算,使用像 =  或 <> 之类的比较运算符).包括相等联接和自然联接.     内联接使用比较运算符根据每个表共有的列的值匹配两个表中的行.例如,检索 stude ...

随机推荐

  1. ipv6 地址说明

    开篇我们先简单介绍下ipv4 地址 IPv4 地址: ipv4地址一共32位,用点分十进制表示,每一个部分是8位.子网掩码有两种表示 192.168.1.3 / 24 表示ip的前24位是网络位,后8 ...

  2. Deepin Linux下为Wine创建文件关联

    在Deepin Linux下,默认地,使用apt安装的Wine并没有创建文件关联,这使得在文件管理器中双击exe等Windows可执行文件时,不能直接运行.为此,必须手动在桌面环境中创建文件关联. 文 ...

  3. 025 Android 带进度条的对话框(ProgressDialog)

    1.ProgressDialog介绍 ProgressDialog可以在当前界面弹出一个置顶于所有界面元素的对话框,同样具有屏蔽其他控件的交互能力,用于提示用户当前操作正在运行,让用户等待: 2.应用 ...

  4. 【Webservice】2 counts of IllegalAnnotationExceptions Two classes have the same XML type name

    在使用客户端调用服务端的时候发生了2 counts of IllegalAnnotationExceptions Two classes have the same XML type name的错误, ...

  5. [C++] 二叉树计算文件单词数

    目录 前置技能 构造和遍历二叉树 文件的打开.读取和写入 需求描述 读取文件 构建二叉树 格式化输入输出 具体实现 main.cpp binarytree.h binarytree.cpp 使用二叉树 ...

  6. 15-16 ICPC europe J Saint John Festival (graham扫描法+旋转卡壳)

    题意:给n个大点,m个小点$(n<=1e5,m<=5e5),问有多少个小点,存在3个大点,使小点在三个大点组成的三角形内. 解题思路: 首先,易证,若该小点在某三大点行成的三角形内,则该小 ...

  7. thinkphp5.0数据导出excel表格

    第一步.创建Model类文件(名称自定) 第二步.在类中写入以下代码 <?php namespace Admin\admin\model; use think\Model; class Mark ...

  8. 20191108-从零搭建基于Linux的RobotFramework框架

    第一步:安装Centos7 64位操作系统 直接安装即可,不详述 第二步:yum下载wget yum update yum install wget 第三步:安装Python3 建议安装3.7,我在 ...

  9. 知识不是来炫耀的,而是来分享的-----现在的人们却…似乎开始变味了…

    我讨厌那些自以为是的人,哪些只有远大抱负却不付出的混蛋,我讨厌那些老生欺负小生,讨厌以大欺小,讨厌别人把知识拿来炫耀. 我自己也不愿做这类人,我渴望看到成功,我不怕一意孤行,我不怕失败,我只怕自己做的 ...

  10. Scratch编程:画多边形(八)

    “ 上节课的内容全部掌握了吗?反复练习了没有,编程最好的学习方法就是练习.练习.再练习.一定要记得多动手.多动脑筋哦~~” 01 — 游戏介绍 这节我们将实现:程序提示用户输入数据,然后根据用户的输入 ...