Mysql之左连接右连接内连接——示例 (转)

  下面是两张表

  表stu

  

  表tech

  

  

  1.右连接

  当使用右连接语句查询时,返回结果如下:

1 SELECT stu.id,stu.name,stu.classe_name,tech.id,tech.name FROM stu RIGHT JOIN tech on stu.classe_name=tech.classe_name;

  

  从结果中可以看出,tech表中被查询的字段会被全部显示出来,而stu表中,只有与表tech的classe_name相同的条目的相应字段才会被显示出来。

  右连接即:返回右边表中所有被查询字段+左边表中符合条件的字段。

  2.左连接

  当使用左连接语句查询时,返回结果如下:

1 SELECT stu.id,stu.name,stu.classe_name,tech.id,tech.name FROM stu LEFT JOIN tech on stu.classe_name=tech.classe_name;

  

  从结果中可以看出,stu表中被查询的字段会被全部显示出来,而tech表中,只有与表stu的classe_name相同的条目的相应字段才会被显示出来。

  左连接即:返回左边表中所有被查询字段+右边表中符合条件的字段。

  3.内连接

  当使用内连接语句查询时,返回结果如下:

1 SELECT stu.id,stu.name,stu.classe_name,tech.id,tech.name FROM stu INNER JOIN tech on stu.classe_name=tech.classe_name;

  

  从结果中可以看出,stu表与tech表中classe_name相同的条目都会被显示出来。

  内连接:返回表中符合条件的条目。

  

 

 
 
分类: Mysql
posted @ 2017-08-23 10:52  yolo_bean  阅读(151)  评论(0编辑  收藏  举报