</pre>弄个例题,直观一点。两个表:<p></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei'; font-size:14px; line-height:28px; white-space:pre-wrap">--表stu</span></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei'; font-size:14px; line-height:28px; white-space:pre-wrap; background-color:rgb(102,102,204)">id name</span></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei'; font-size:14px; line-height:28px; white-space:pre-wrap; background-color:rgb(102,102,204)">1, Jack</span></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei'; font-size:14px; line-height:28px; white-space:pre-wrap; background-color:rgb(102,102,204)">2, Tom</span></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei'; font-size:14px; line-height:28px; white-space:pre-wrap; background-color:rgb(102,102,204)">3, Kity</span></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei'; font-size:14px; line-height:28px; white-space:pre-wrap; background-color:rgb(102,102,204)">4, nono</span></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei'; font-size:14px; line-height:28px; white-space:pre-wrap">--表exam</span></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei'; font-size:14px; line-height:28px; white-space:pre-wrap; background-color:rgb(102,51,255)">id grade</span></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei'; font-size:14px; line-height:28px; white-space:pre-wrap; background-color:rgb(102,51,255)">1, 56</span></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei'; font-size:14px; line-height:28px; white-space:pre-wrap; background-color:rgb(102,51,255)">2, 76</span></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei'; font-size:14px; line-height:28px; white-space:pre-wrap; background-color:rgb(102,51,255)">11, 89</span></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei'; font-size:14px; line-height:28px; white-space:pre-wrap">内连接 (显示两表id匹配的)</span></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei'; font-size:14px; line-height:28px; white-space:pre-wrap"></span></p><pre name="code" class="sql">select stu.id,exam.id,stu.name, exam.grade from stu inner join exam on stu.id=exam.id
stu.id exam.id name grade
--------------------------------
1 1 Jack 56
2 2 Tom 76
左连接(显示join 左边的表的所有数据,exam只有两条记录,所以stu.id,grade 都用NULL 显示)
select stu.id,exam.id,stu.name, exam.grade from stu left join exam on stu.id=exam.id
1 1 Jack 56
2 2 Tom 76
3 NULL Kity NULL
4 NULL nono NULL
右连接(与作连接相反,显示join右边表的所有数据)
select stu.id,exam.id,stu.name, exam.grade from stu right join exam on stu.id=exam.id
1 1 Jack 56
2 2 Tom 76
NULL 11 NULL 89