1.基礎表student-學生表,class--班級表
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIiclRnblN0LclHdpZXYyd2LcBzNvwVZ2x2bzNXak9CX90TQNNkRrFlQKBTSvwFbslmZvwFMwQzLcVmepNHdu9mZvwFVywUNMZTY18CX052bm9CX9MGRPFTWq5UNVpWTmZEWjZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39TN3ADOygjM0ETMxgDM3EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
2.内部關聯,兩種方式
2.1.使用“=”或者“<>”符号
select class.name as class_name ,student.name as student_name from class,student where class.id=student.class_id
2.2.使用inner join進行表關聯
select class.name as class_name ,student.name as student_name from class inner join student on class.id=student.class_id
傳回結果相同為:
分析結果:兩張表進行笛卡爾積運算之後篩選出條件相符的資料。
說明:笛卡爾積運算(a,b)(1,2,3)運算結果為(a,1)(a,2),(a,3)(b,1)(b,2)(b,3),其中兩個基礎表的笛卡爾積結果為
3.左關聯 left join
select class.name as class_name ,student.name as student_name from class left join student on class.id=student.class_id
運作結果
分析結果:左邊表資料全部顯示,當在右邊表找不到結果是,字段内容為null
4.右關聯 right join
select class.name as class_name ,student.name as student_name from class right join student on class.id=student.class_id
分析結果:右邊表資料全部顯示,當在左邊表找不到結果是,字段内容為null
注:自我了解,如若不對請指教。