1、首先创建student库

  1. create database student;

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

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

① 对boy表插入部分数据

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

② 对girl表插入部分数据

  1. mysql> insert into girl(girlName,age) values('刘亦菲',31);
  2. mysql> insert into girl(girlName,age) values('唐嫣',35);
  3. mysql> insert into girl(girlName,age) values('刘诗诗',38);
  4. mysql> insert into girl(girlName,age) values('马苏',30);
  5. mysql> insert into girl(girlName,age) values('杨幂',39);
  6. mysql> insert into girl(girlName,age) values('赵丽颖',29);
  1. mysql> select * from girl;
  2. +--------+----------+-----+-----------+
  3. | girlid | girlName | age | connectId |
  4. +--------+----------+-----+-----------+
  5. | 101 | 刘亦菲 | 31 | NULL |
  6. | 102 | 唐嫣 | 35 | NULL |
  7. | 103 | 刘诗诗 | 38 | NULL |
  8. | 104 | 马苏 | 30 | NULL |
  9. | 105 | 杨幂 | 39 | NULL |
  10. | 106 | 赵丽颖 | 29 | NULL |
  11. +--------+----------+-----+-----------+

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

① boyId=2 连接 girlId = 102

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

② boyId=3 连接 girlId = 104

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

③  boyId=6 连接 girlId = 105

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

得出boy表

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

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

① girlId=101 连接 boyd = 3

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

② girlId=103 连接 boyd = 4

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

③ girlId=105 连接 boyd = 6

  1. update girl set connectId=6 where girlId=105;

得出girl表

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

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

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

  boy表连接girl表

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

  girl表连接boy表

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

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

  boy - > girl

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

  girl - > boy

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

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

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

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

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

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

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

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. 1.IO的演进

      1.Java IO 演进之路 本文围绕着一下几个问题 1.Java 中 BIO.NIO.AIO 之间的区别及应用场景. 2.阻塞(Block)与非阻塞(Non-Block)区别. 3.同步(Syn ...

  2. CSS的三种基本框架

    CSS的三类选择器 1.css-css的基本选择器(三种) 要对哪个标签里面的数据进行操作 (1)标签选择器 div { background-color:red; color:blue; } (2) ...

  3. Python【input()函数】

    运用input函数搜集信息 input()函数结果的赋值name = input('请输入你的名字:') #将input()函数的执行结果(收集的信息)赋值给变量name input()函数的使用场景 ...

  4. Python re模块前的正则表达式常用语法小总结

    一.正则表达式: (1).正则表达式是干什么的  正则表达式(regular expression)描述了一种字符串匹配的模式(pattern),可以用来检查一个串是否含有某种子串.将匹配的子串替换或 ...

  5. Eclipse设置每行的最大字符数

    Eclipse默认宽度是 120 个字符.如下图所示(提示:格式化快捷键Ctrl + Shift + F): 设置步骤如下: 菜单栏倒数第二项,选择Window 下拉栏最后一项,选择Preferenc ...

  6. SQLSERVER查询存储过程内容

    --使用语句查看一个存储过程的定义 EXEC sp_helptext 'Auth_BankCardAuthorize' --查询所有存储过程的名称以及定义 SELECT name, definitio ...

  7. truncate删除一个分区,测试全局索引是否失效

    目的,有一个清理数据的需求,需要删除历史的一个分区所有记录信息,但是存在主键global索引,如何更好的维护. 如下测试流程一 提前创建好一个已时间created 字段作为分区键的范围分区表 SQL& ...

  8. 线程三(Mutex)

    C# 中 Mutex 类也是用于线程同步操作的类,例如,当多个线程同时访问一个资源时保证一次只能有一个线程访问资源. 在 Mutex 类中,WaitOne() 方法用于等待资源被释放, Release ...

  9. PCA(Principal Component Analysis)笔记

    PCA是机器学习中recognition中的传统方法,今天下午遇到了,梳理记一下 提出背景: 二维空间里,2个相近的样本,有更大概率具有相同的属性,但是在高维空间里,由于样本在高维空间里,呈现越来越稀 ...

  10. log4net SmtpAppender 踩坑总结

    错误集合: System.Net.Mail.SmtpException: 命令顺序不正确. 服务器响应为:Error: need EHLO and AUTH first ! System.Net.Ma ...