转载请注明出处!!
之前数据表连接操作多使用逗号或者join,对几种连接的概念一直浑浑噩噩,最近研究了一波,把这些连接的区别搞明白了。
 
连接:A xjoin B(主表 操作 关联表)
 select过程:from->where->group by->having->order by->limit
在不使用on语法时,join、inner join、逗号、cross join结果相同,都是取2个表的笛卡尔积。逗号与其他操作符优先级不同,所以有可能产生语法错误,尽量减少用逗号
 
join、inner join、cross join支持on和using语法,逗号不支持on和using语法
on语法:筛选连接后的结果,两表的对应列值相同才在结果集中,可以通过and连接多个列值的匹配要求,列名可以不同
     select * from tb_test1 inner join tb_student on tb_test1.id=tb_student.id;
using语法:筛选连接后的结果,两表的对应列值相同才在结果集中,括号内用多个列名要求用逗号连接,列名必须相同
     select * from tb_test1 cross join tb_student using(id);
 
仅MySQL中!:(MySQL中没有全外连接,用UNION操作实现吧)
join=inner join=cross join(笛卡尔积)
left join=left outer join
right join=right outer join
 
sample数据:
 
 select * from tb_test1;
+----+-----------+--------+-------+
| id | name      | gender | score |
+----+-----------+--------+-------+
|  1 | 李毅      | 男     | 95.30 |
|  2 | 二丫      | 女     | 95.30 |
|  3 | 张三      | 女     | 95.30 |
|  4 | 李四      | 女     | 95.30 |
|  7 | 胡鲁瓦    | 男     | 95.30 |
|  9 | 后羿      | 男     | 95.30 |
+----+-----------+--------+-------+
 
 select * from tb_student;
+----+--------+--------+------------+
| id | name   | gender | birthday   |
+----+--------+--------+------------+
|  1 | 李毅   | 男     | 1988-03-20 |
|  2 | kevin  | 男     | 1987-08-23 |
|  3 | marry  | 女     | 1989-11-25 |
|  4 | lucy   | 女     | 1989-11-25 |
|  5 | lily   | 女     | 1992-01-25 |
+----+--------+--------+------------+
 

natural join:自然连接(不允许带on/using)
     natrual join:全自然连接,对左右2个表相同名字的列进行等值匹配,不可用on,using操作指定,自动删除多余重名列
     natrual left join:左自然连接,保留2个表的列(删除多余重名列),以左表为准,不存在匹配的右表列,值置为NULL
     natrual right join:和左自然连接相反
 
select * from tb_test1 natural join tb_student;
+----+--------+--------+-------+------------+
| id | name   | gender | score | birthday   |
+----+--------+--------+-------+------------+
|  1 | 李毅   | 男     | 95.30 | 1988-03-20 |
+----+--------+--------+-------+------------+
 
select * from tb_test1 natural left join tb_student;
+----+-----------+--------+-------+------------+
| id | name      | gender | score | birthday   |
+----+-----------+--------+-------+------------+
|  1 | 李毅      | 男     | 95.30 | 1988-03-20 |
|  2 | 二丫      | 女     | 95.30 | NULL       |
|  3 | 张三      | 女     | 95.30 | NULL       |
|  4 | 李四      | 女     | 95.30 | NULL       |
|  7 | 胡鲁瓦    | 男     | 95.30 | NULL       |
|  9 | 后羿      | 男     | 95.30 | NULL       |
+----+-----------+--------+-------+------------+
 
select * from tb_test1 natural right join tb_student;
+----+--------+--------+------------+-------+
| id | name   | gender | birthday   | score |
+----+--------+--------+------------+-------+
|  1 | 李毅   | 男     | 1988-03-20 | 95.30 |
|  2 | kevin  | 男     | 1987-08-23 |  NULL |
|  3 | marry  | 女     | 1989-11-25 |  NULL |
|  4 | lucy   | 女     | 1989-11-25 |  NULL |
|  5 | lily   | 女     | 1992-01-25 |  NULL |
+----+--------+--------+------------+-------+
 

inner join:内连接(不带on/using时做笛卡尔积)
     主表和关联表的笛卡尔积结果,通过on或者using操作筛选结果集(先求笛卡尔积,后筛选,所以性能上和where一样),2个表的所有列都被保存
 
 select * from tb_student a join tb_test1 b on a.id=b.id;
+----+--------+--------+------------+----+--------+--------+-------+
| id | name   | gender | birthday   | id | name   | gender | score |
+----+--------+--------+------------+----+--------+--------+-------+
|  1 | 李毅   | 男     | 1988-03-20 |  1 | 李毅   | 男     | 95.30 |
|  2 | kevin  | 男     | 1987-08-23 |  2 | 二丫   | 女     | 95.30 |
|  3 | marry  | 女     | 1989-11-25 |  3 | 张三   | 女     | 95.30 |
|  4 | lucy   | 女     | 1989-11-25 |  4 | 李四   | 女     | 95.30 |
+----+--------+--------+------------+----+--------+--------+-------+
 
select * from tb_student a join tb_test1 b on a.gender=b.gender and a.name=b.name;
+----+--------+--------+------------+----+--------+--------+-------+
| id | name   | gender | birthday   | id | name   | gender | score |
+----+--------+--------+------------+----+--------+--------+-------+
|  1 | 李毅   | 男     | 1988-03-20 |  1 | 李毅   | 男     | 95.30 |
|  6 | 李毅   | 男     | 1985-10-11 |  1 | 李毅   | 男     | 95.30 |
+----+--------+--------+------------+----+--------+--------+-------+
 
 

left join:左外连接(必须带on/using,等价与left outer join)
     左表为主表,保留全部记录全部列,右表为关联表,加上on操作之外的其他列,符合on条件的记录连接上
 
on和where的区别(在外连接时有差别):on和where在性能上不存在差异,都是在笛卡尔积后筛选,但on在from阶段,属于join操作,先于where。所以当left join连接时,虽然左表的列不符合on条件,也必须保留,只是该记录右表全是null。而where对连接后的结果做筛选,不符合条件的直接剔除
 
select * from tb_student a left join tb_test1 b on a.id=b.id;
+----+--------+--------+------------+------+--------+--------+-------+
| id | name   | gender | birthday   | id   | name   | gender | score |
+----+--------+--------+------------+------+--------+--------+-------+
|  1 | 李毅   | 男     | 1988-03-20 |    1 | 李毅   | 男     | 95.30 |
|  2 | kevin  | 男     | 1987-08-23 |    2 | 二丫   | 女     | 95.30 |
|  3 | marry  | 女     | 1989-11-25 |    3 | 张三   | 女     | 95.30 |
|  4 | lucy   | 女     | 1989-11-25 |    4 | 李四   | 女     | 95.30 |
|  5 | lily   | 女     | 1992-01-25 | NULL | NULL   | NULL   |  NULL |
|  6 | 李毅   | 男     | 1985-10-11 | NULL | NULL   | NULL   |  NULL |
+----+--------+--------+------------+------+--------+--------+-------+
 
select * from tb_student a left join tb_test1 b on a.id=b.id and a.id<4;
+----+--------+--------+------------+------+--------+--------+-------+
| id | name   | gender | birthday   | id   | name   | gender | score |
+----+--------+--------+------------+------+--------+--------+-------+
|  1 | 李毅   | 男     | 1988-03-20 |    1 | 李毅   | 男     | 95.30 |
|  2 | kevin  | 男     | 1987-08-23 |    2 | 二丫   | 女     | 95.30 |
|  3 | marry  | 女     | 1989-11-25 |    3 | 张三   | 女     | 95.30 |
|  4 | lucy   | 女     | 1989-11-25 | NULL | NULL   | NULL   |  NULL |
|  5 | lily   | 女     | 1992-01-25 | NULL | NULL   | NULL   |  NULL |
|  6 | 李毅   | 男     | 1985-10-11 | NULL | NULL   | NULL   |  NULL |
+----+--------+--------+------------+------+--------+--------+-------+
 
select * from tb_student a left join tb_test1 b on a.id=b.id where a.id<4;
+----+--------+--------+------------+------+--------+--------+-------+
| id | name   | gender | birthday   | id   | name   | gender | score |
+----+--------+--------+------------+------+--------+--------+-------+
|  1 | 李毅   | 男     | 1988-03-20 |    1 | 李毅   | 男     | 95.30 |
|  2 | kevin  | 男     | 1987-08-23 |    2 | 二丫   | 女     | 95.30 |
|  3 | marry  | 女     | 1989-11-25 |    3 | 张三   | 女     | 95.30 |
+----+--------+--------+------------+------+--------+--------+-------+
 

right join:右外连接,和左外连接相反
 
 
以后再使用MySQL的连接就清晰了。
如有不准确的地方请留言指教
 

MySQL的几种连接 join/inner join/cross join/逗号/left join/right join/natural join的更多相关文章

  1. mysql的三种连接方式

    SQL的三种连接方式分为:左外连接.右外连接.内连接,专业术语分别为:LEFT JOIN.RIGHT JOING.INNER JOIN 内连接INNER JOIN:使用比较运算符来根据指定的连接的每个 ...

  2. 比较详细的mysql的几种连接功能分析,只要你看完就能学会的好东西

    下面是例子分析表A记录如下: aID        aNum 1           a20050111 2           a20050112 3           a20050113 4   ...

  3. 170221、浅谈mysql的SQL的四种连接

    例子:   -------------------------------------------------  a表     id   name     b表     id   job   pare ...

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

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

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

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

  6. 深入了解SQL的四种连接&不然要命的!

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

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

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

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

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

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

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

随机推荐

  1. linux 下部署 redis

    Redis是一种高级key-value数据库.它跟memcached类似,不过数据 可以持久化,而且支持的数据类型很丰富.有字符串,链表,集 合和有序集合.支持在服务器端计算集合的并,交和补集(dif ...

  2. asp.net用url重写URLReWriter实现任意二级域名

    本文转自 http://www.cnblogs.com/notus/archive/2007/03/13/673222.html

  3. andorid 远程存储中JDK和Volley的GET和POST方法

    在操作Volley的时候先添加volley.jar(包) <uses-permission android:name="android.permission.INTERNET" ...

  4. [转]Sql server2005中如何格式化时间日期

    ) -- mon dd yyyy hh:mmAM (or PM) ) -- mm/dd/yyyy - 10/02/2008 ) -- yyyy.mm.dd -- 2008.10.02 ) -- dd/ ...

  5. IIS访问提示您不具备查看该目录或页面的权限,因为访问控制列表 (ACL) 对 Web 服务器上的该资源进行了配置

    1. 检查IIS权限,webmail目录权限. 2. 在本地安全策略中,设置从网络访问本地服务器与拒绝从网络访问本地服务器.中查看相关用户的权限 3. 如果上面的步骤还是有问题.使用下面的步骤进行设置 ...

  6. linux 清理内存命令 查看内存命令

    查看内存: 我们可以用free命令查看内存信息: free -g total used free shared buffers cachedMem: 15   15    0     0       ...

  7. VB.NET上传附件代码

    '附件添加 按钮 点击事件 吴翰哲 2013年7月23日 16:53:19 Protected Sub BtnAddFile_Click(ByVal sender As Object, ByVal e ...

  8. [VS2013]如何闪开安装VS2013必须要有安装IE10的限制

    来源:http://blog.163.com/qimo601@126/blog/static/1582209320143354446462/   已阻止安装程序,此版本的Visual Studio需要 ...

  9. php获得ip地址

    方法一: <?phpfunction GetIP(){if(!empty($_SERVER["HTTP_CLIENT_IP"])){ $cip = $_SERVER[&quo ...

  10. Failed to execute goal on project MakeFriends: Could not resolve dependencie The POM for .chengpai.jtd:jtd-service-api:jar:1.0-SNAPSHOT is missing, no dependency information available

    本笔者在学习maven的基础,然后建立了一个maven的项目,然后想对其进行依赖操作,pom.xml进行依赖操作时候出现了这样的错误,说是找不到这个依赖的包,但是事实上已经导入了这个包. 同时,也在m ...