Home > Database > Mysql Tutorial > body text

Mysql之左连接右连接内连接――示例_MySQL

WBOY
Release: 2016-06-01 13:32:19
Original
882 people have browsed it

bitsCN.com


 

很少用到数据库,基本上只会简单的增删选改。前些日,面试的时候被问到左连接右连接,然后...然后就没有然后了。
网上搜了一些资料,自己又示例操作了一遍,总算有点明白了。现在记录于此,以便日后查阅。
话不多说,请看示例。
下面是两张表
表stu
Mysql之左连接右连接内连接――示例_MySQL
表tech
Mysql之左连接右连接内连接――示例_MySQL

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;
Copy after login

Mysql之左连接右连接内连接――示例_MySQL
从结果中可以看出,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;
Copy after login

Mysql之左连接右连接内连接――示例_MySQL
从结果中可以看出,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;
Copy after login

 

Mysql之左连接右连接内连接――示例_MySQL
从结果中可以看出,stu表与tech表中classe_name相同的条目都会被显示出来。
内连接:返回表中符合条件的条目。


bitsCN.com
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!