首先先建兩個表,student表和score表
select * from student;
student表數據如下:
select * from score;
score表數據如下:
可以看到students表中stu_id為16048008的記錄對應score表沒有數據;
1.內連接只顯示兩表中有關聯的數據
select * from student inner join score on student.sid = score.stu_id;
從表中可以看出student表中sid=16048008,sid=16048009,sid=160480010在score表中沒有對應數據,所以內連接的結果不顯示這三名學生
2.左連接顯示左表所有數據,右表沒有對應的數據用NULL補齊,多了的數據刪除
select * from student left join score on student.sid = score.stu_id;
從結果可以看出sid=16048008,sid=16048009,sid=160480010在score表中沒有數據的部分用NULL代替
3.右連接顯示右表所有數據,左表沒有對應的數據用NULL對齊,多了的數據刪除
select * from student right join score on student.sid = score.stu_id;
score表中沒有的數據student表也不顯示