在 mysql 查询语句中,JOIN 扮演的角色很重要,所以掌握其用法很重要。很多同学可能只是会用几种常用的,但要成为高级的工程师是需要掌握透彻,360度全无死角。
图片精华版
![一张图搞定七种JOIN关系 一张图搞定七种 JOIN 关系_第1张图片](http://img.e-com-net.com/image/info9/43e4bba5d7964b3e970805ec4da68843.jpg)
文字解释版
1. 需要准备好两个table:subject(学科表)和 student_score(学生成绩表)
通过学生成绩表的subject_id字段(学科ID)和学科表的id字段(主键ID)进行关联
![一张图搞定七种JOIN关系 一张图搞定七种 JOIN 关系_第2张图片](http://img.e-com-net.com/image/info9/3a0c12b740ef487294fafea16ce468f2.jpg)
![一张图搞定七种JOIN关系 一张图搞定七种 JOIN 关系_第3张图片](http://img.e-com-net.com/image/info9/60300b924ac549d7894327f7be757b91.jpg)
2. 分别填充数据
![一张图搞定七种JOIN关系 一张图搞定七种 JOIN 关系_第4张图片](http://img.e-com-net.com/image/info9/8a743d0af8c5478b85742ce8eafb5268.jpg)
![一张图搞定七种JOIN关系 一张图搞定七种 JOIN 关系_第5张图片](http://img.e-com-net.com/image/info9/a089a0c170e344819d03962dc9fee58a.jpg)
3. inner join
语句:select score.student_name,score.score,subject.name,subject.teacher from student_score as score inner join subject on score.subject_id = subject.id;
![一张图搞定七种JOIN关系 一张图搞定七种 JOIN 关系_第6张图片](http://img.e-com-net.com/image/info9/0902c0b8b7e1461db470943d17048698.jpg)
4. left join (共有+右表不匹配补NULL)
语句:select score.student_name,score.score,subject.name,subject.teacher from student_score as score left join subject on score.subject_id = subject.id;
![一张图搞定七种JOIN关系 一张图搞定七种 JOIN 关系_第7张图片](http://img.e-com-net.com/image/info9/dc63291bc0fd47bda0f7f253a816282f.jpg)
5. left join (左表独有)
语句:select score.student_name,score.score,subject.name,subject.teacher from student_score as score left join subject on score.subject_id = subject.id where subject.id is null;
![一张图搞定七种JOIN关系 一张图搞定七种 JOIN 关系_第8张图片](http://img.e-com-net.com/image/info9/38cdf3d28b5c422281f231a2169b3ac1.jpg)
6. right join (共有+左表不匹配补NULL)
语句:select score.student_name,score.score,subject.name,subject.teacher from student_score as score right join subject on score.subject_id = subject.id;
![一张图搞定七种JOIN关系 一张图搞定七种 JOIN 关系_第9张图片](http://img.e-com-net.com/image/info9/16e0a2743df5494c9dd57be4a59220d0.jpg)
7. right join (右表独有)
语句:select score.student_name,score.score,subject.name,subject.teacher from student_score as score right join subject on score.subject_id = subject.id where score.id is null;
![一张图搞定七种JOIN关系 一张图搞定七种 JOIN 关系_第10张图片](http://img.e-com-net.com/image/info9/bcb14311aefb40f4923e90500a673a76.jpg)
8. union (左右表合并并去重)
语句:
select score.student_name,score.score,subject.name,subject.teacher from student_score as score left join subject on score.subject_id = subject.id
union
select score.student_name,score.score,subject.name,subject.teacher from student_score as score right join subject on score.subject_id = subject.id;
![一张图搞定七种JOIN关系 一张图搞定七种 JOIN 关系_第11张图片](http://img.e-com-net.com/image/info9/832580ed52884b80955bc55936dedbc6.jpg)
9. union (左右表独有)
语句:
select score.student_name,score.score,subject.name,subject.teacher from student_score as score left join subject on score.subject_id = subject.id where subject.id is null
union
select score.student_name,score.score,subject.name,subject.teacher from student_score as score right join subject on score.subject_id = subject.id where score.id is null;
![一张图搞定七种JOIN关系 一张图搞定七种 JOIN 关系_第12张图片](http://img.e-com-net.com/image/info9/ca46168670a24a99965d7f0c49bc9fb5.jpg)