Die Instanzdatenbank lautet wie folgt:
Studententabelle:
mysql> select * from student; +-----------+-----------+------+------+-------+ | Sno | Sname | Ssex | Sage | Sdept | +-----------+-----------+------+------+-------+ | 201215121 | 李勇 | 男 | 22 | CS | | 201215122 | 刘晨 | 女 | 19 | CS | | 201215123 | 王敏 | 女 | 18 | MA | | 201215125 | 张立 | 男 | 19 | IS | | 201215128 | 陈冬 | 男 | 18 | IS | | 201215126 | 张成民 | 男 | 18 | CS | +-----------+-----------+------+------+-------+6 rows in set (0.00 sec)
sc-Tabelle:
mysql> select * from sc; +-----------+------+-------+ | Sno | Cno | Grade | +-----------+------+-------+ | 201215121 | 1 | 92 | | 201215121 | 2 | 85 | | 201215121 | 3 | 88 | | 201215122 | 2 | 90 | | 201215122 | 3 | 80 | | 201215128 | 1 | 78 | +-----------+------+-------+6 rows in set (0.00 sec)
Die SQL-Anweisung lautet wie folgt:
select * from student left join sc on student.Sno=sc.Sno;
Die laufenden Ergebnisse sind wie folgt:
+-----------+-----------+------+------+-------+-----------+------+-------+ | Sno | Sname | Ssex | Sage | Sdept | Sno | Cno | Grade | +-----------+-----------+------+------+-------+-----------+------+-------+ | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 1 | 92 | | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 2 | 85 | | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 3 | 88 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 2 | 90 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 3 | 80 | | 201215128 | 陈冬 | 男 | 18 | IS | 201215128 | 1 | 78 | | 201215123 | 王敏 | 女 | 18 | MA | NULL | NULL | NULL | | 201215125 | 张立 | 男 | 19 | IS | NULL | NULL | NULL | | 201215126 | 张成民 | 男 | 18 | CS | NULL | NULL | NULL | +-----------+-----------+------+------+-------+-----------+------+-------+
In diesem Beispiel basiert der Left Join auf den Datensätzen in der Student-Tabelle. Die Student-Tabelle kann als linke Tabelle betrachtet werden, und die sc-Tabelle kann es sein Die Datensätze in der linken Tabelle werden vollständig angezeigt. Wenn die rechte Tabelle nicht mit der linken Tabelle übereinstimmt, werden die verbleibenden Teile als Null angezeigt.
Die using-Klausel ähnelt der on-Klausel, die Ergebnisse unterscheiden sich jedoch geringfügig.
Zum Beispiel:
mysql> select student.Sno,Sname,Grade from student left join sc on student.Sno=sc.Sno; +-----------+-----------+-------+ | Sno | Sname | Grade | +-----------+-----------+-------+ | 201215121 | 李勇 | 92 | | 201215121 | 李勇 | 85 | | 201215121 | 李勇 | 88 | | 201215122 | 刘晨 | 90 | | 201215122 | 刘晨 | 80 | | 201215128 | 陈冬 | 78 | | 201215123 | 王敏 | NULL | | 201215125 | 张立 | NULL | | 201215126 | 张成民 | NULL | +-----------+-----------+-------+ 9 rows in set (0.00 sec)
Das Obige entspricht
select Sno,Sname,Grade from student left join sc using(Sno); +-----------+-----------+-------+ | Sno | Sname | Grade | +-----------+-----------+-------+ | 201215121 | 李勇 | 92 | | 201215121 | 李勇 | 85 | | 201215121 | 李勇 | 88 | | 201215122 | 刘晨 | 90 | | 201215122 | 刘晨 | 80 | | 201215128 | 陈冬 | 78 | | 201215123 | 王敏 | NULL | | 201215125 | 张立 | NULL | | 201215126 | 张成民 | NULL | +-----------+-----------+-------+
an verschiedenen Stellen, zum Beispiel:
select * from student left join sc on student.Sno=sc.Sno; +-----------+-----------+------+------+-------+-----------+------+-------+ | Sno | Sname | Ssex | Sage | Sdept | Sno | Cno | Grade | +-----------+-----------+------+------+-------+-----------+------+-------+ | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 1 | 92 | | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 2 | 85 | | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 3 | 88 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 2 | 90 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 3 | 80 | | 201215128 | 陈冬 | 男 | 18 | IS | 201215128 | 1 | 78 | | 201215123 | 王敏 | 女 | 18 | MA | NULL | NULL | NULL | | 201215125 | 张立 | 男 | 19 | IS | NULL | NULL | NULL | | 201215126 | 张成民 | 男 | 18 | CS | NULL | NULL | NULL | +-----------+-----------+------+------+-------+-----------+------+-------+
select * from student left join sc using (sno); +-----------+-----------+------+------+-------+------+-------+ | Sno | Sname | Ssex | Sage | Sdept | Cno | Grade | +-----------+-----------+------+------+-------+------+-------+ | 201215121 | 李勇 | 男 | 22 | CS | 1 | 92 | | 201215121 | 李勇 | 男 | 22 | CS | 2 | 85 | | 201215121 | 李勇 | 男 | 22 | CS | 3 | 88 | | 201215122 | 刘晨 | 女 | 19 | CS | 2 | 90 | | 201215122 | 刘晨 | 女 | 19 | CS | 3 | 80 | | 201215128 | 陈冬 | 男 | 18 | IS | 1 | 78 | | 201215123 | 王敏 | 女 | 18 | MA | NULL | NULL | | 201215125 | 张立 | 男 | 19 | IS | NULL | NULL | | 201215126 | 张成民 | 男 | 18 | CS | NULL | NULL | +-----------+-----------+------+------+-------+------+-------+
Sno duplizieren Spalte. Wenn das On-Wort verwendet wird, wird es zweimal ausgegeben.
ist dasselbe wie LEFT JOIN, basiert jedoch auf der rechten Tabelle, zum Beispiel:
select * from student right join sc using (sno); +-----------+------+-------+--------+------+------+-------+ | Sno | Cno | Grade | Sname | Ssex | Sage | Sdept | +-----------+------+-------+--------+------+------+-------+ | 201215121 | 1 | 92 | 李勇 | 男 | 22 | CS | | 201215121 | 2 | 85 | 李勇 | 男 | 22 | CS | | 201215121 | 3 | 88 | 李勇 | 男 | 22 | CS | | 201215122 | 2 | 90 | 刘晨 | 女 | 19 | CS | | 201215122 | 3 | 80 | 刘晨 | 女 | 19 | CS | | 201215128 | 1 | 78 | 陈冬 | 男 | 18 | IS | +-----------+------+-------+--------+------+------+-------+
zeigt nicht an, wer darauf basiert, sondern nur die Datensätze, die die Bedingungen erfüllen angezeigt
select * from student inner join sc on student.Sno=sc.Sno; +-----------+--------+------+------+-------+-----------+------+-------+ | Sno | Sname | Ssex | Sage | Sdept | Sno | Cno | Grade | +-----------+--------+------+------+-------+-----------+------+-------+ | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 1 | 92 | | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 2 | 85 | | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 3 | 88 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 2 | 90 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 3 | 80 | | 201215128 | 陈冬 | 男 | 18 | IS | 201215128 | 1 | 78 | +-----------+--------+------+------+-------+-----------+------+-------+
Die obige Anweisung entspricht:
select * from student,sc where student.Sno=sc.Sno; +-----------+--------+------+------+-------+-----------+------+-------+ | Sno | Sname | Ssex | Sage | Sdept | Sno | Cno | Grade | +-----------+--------+------+------+-------+-----------+------+-------+ | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 1 | 92 | | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 2 | 85 | | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 3 | 88 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 2 | 90 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 3 | 80 | | 201215128 | 陈冬 | 男 | 18 | IS | 201215128 | 1 | 78 | +-----------+--------+------+------+-------+-----------+------+-------+
Sie können eine Where-Anweisung
select * from student left join sc using(Sno) where sc.Sno is null; +-----------+-----------+------+------+-------+------+-------+ | Sno | Sname | Ssex | Sage | Sdept | Cno | Grade | +-----------+-----------+------+------+-------+------+-------+ | 201215123 | 王敏 | 女 | 18 | MA | NULL | NULL | | 201215125 | 张立 | 男 | 19 | IS | NULL | NULL | | 201215126 | 张成民 | 男 | 18 | CS | NULL | NULL | +-----------+-----------+------+------+-------+------+-------+
kann mit Union-Klauseln kombiniert werden, da in diesem Beispiel die richtigen Alle übereinstimmen, sodass das angezeigte Ergebnis mit dem vorherigen übereinstimmt.
select * from student left join sc using(Sno) where student.Sno is null union select * from student left join sc using(Sno) where sc.Sno is null; +-----------+-----------+------+------+-------+------+-------+ | Sno | Sname | Ssex | Sage | Sdept | Cno | Grade | +-----------+-----------+------+------+-------+------+-------+ | 201215123 | 王敏 | 女 | 18 | MA | NULL | NULL | | 201215125 | 张立 | 男 | 19 | IS | NULL | NULL | | 201215126 | 张成民 | 男 | 18 | CS | NULL | NULL | +-----------+-----------+------+------+-------+------+-------+
select * from student left join sc on student.Sno=sc.Sno union select * from student right join sc on student.Sno=sc.Sno; +-----------+-----------+------+------+-------+-----------+------+-------+ | Sno | Sname | Ssex | Sage | Sdept | Sno | Cno | Grade | +-----------+-----------+------+------+-------+-----------+------+-------+ | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 1 | 92 | | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 2 | 85 | | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 3 | 88 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 2 | 90 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 3 | 80 | | 201215128 | 陈冬 | 男 | 18 | IS | 201215128 | 1 | 78 | | 201215123 | 王敏 | 女 | 18 | MA | NULL | NULL | NULL | | 201215125 | 张立 | 男 | 19 | IS | NULL | NULL | NULL | | 201215126 | 张成民 | 男 | 18 | CS | NULL | NULL | NULL | +-----------+-----------+------+------+-------+-----------+------+-------+
Hinweis: Ein Links-Join B entspricht dem B-Rechts-Join A
mysql> select * from student left join sc using(Sno); +-----------+-----------+------+------+-------+------+-------+ | Sno | Sname | Ssex | Sage | Sdept | Cno | Grade | +-----------+-----------+------+------+-------+------+-------+ | 201215121 | 李勇 | 男 | 22 | CS | 1 | 92 | | 201215121 | 李勇 | 男 | 22 | CS | 2 | 85 | | 201215121 | 李勇 | 男 | 22 | CS | 3 | 88 | | 201215122 | 刘晨 | 女 | 19 | CS | 2 | 90 | | 201215122 | 刘晨 | 女 | 19 | CS | 3 | 80 | | 201215128 | 陈冬 | 男 | 18 | IS | 1 | 78 | | 201215123 | 王敏 | 女 | 18 | MA | NULL | NULL | | 201215125 | 张立 | 男 | 19 | IS | NULL | NULL | | 201215126 | 张成民 | 男 | 18 | CS | NULL | NULL | +-----------+-----------+------+------+-------+------+-------+ 9 rows in set (0.00 sec)mysql> select * from sc right join student using(Sno); +-----------+-----------+------+------+-------+------+-------+ | Sno | Sname | Ssex | Sage | Sdept | Cno | Grade | +-----------+-----------+------+------+-------+------+-------+ | 201215121 | 李勇 | 男 | 22 | CS | 1 | 92 | | 201215121 | 李勇 | 男 | 22 | CS | 2 | 85 | | 201215121 | 李勇 | 男 | 22 | CS | 3 | 88 | | 201215122 | 刘晨 | 女 | 19 | CS | 2 | 90 | | 201215122 | 刘晨 | 女 | 19 | CS | 3 | 80 | | 201215128 | 陈冬 | 男 | 18 | IS | 1 | 78 | | 201215123 | 王敏 | 女 | 18 | MA | NULL | NULL | | 201215125 | 张立 | 男 | 19 | IS | NULL | NULL | | 201215126 | 张成民 | 男 | 18 | CS | NULL | NULL | +-----------+-----------+------+------+-------+------+-------+
Die Instanzdatenbank sieht wie folgt aus:
Student-Tabelle:
mysql> select * from student; +-----------+-----------+------+------+-------+ | Sno | Sname | Ssex | Sage | Sdept | +-----------+-----------+------+------+-------+ | 201215121 | 李勇 | 男 | 22 | CS | | 201215122 | 刘晨 | 女 | 19 | CS | | 201215123 | 王敏 | 女 | 18 | MA | | 201215125 | 张立 | 男 | 19 | IS | | 201215128 | 陈冬 | 男 | 18 | IS | | 201215126 | 张成民 | 男 | 18 | CS | +-----------+-----------+------+------+-------+ 6 rows in set (0.00 sec)
sc-Tabelle:
mysql> select * from sc; +-----------+------+-------+ | Sno | Cno | Grade | +-----------+------+-------+ | 201215121 | 1 | 92 | | 201215121 | 2 | 85 | | 201215121 | 3 | 88 | | 201215122 | 2 | 90 | | 201215122 | 3 | 80 | | 201215128 | 1 | 78 | +-----------+------+-------+ 6 rows in set (0.00 sec)
Die SQL-Anweisung lautet wie folgt:
select * from student left join sc on student.Sno=sc.Sno;
Das laufende Ergebnis lautet wie folgt:
+-----------+-----------+------+------+-------+-----------+------+-------+ | Sno | Sname | Ssex | Sage | Sdept | Sno | Cno | Grade | +-----------+-----------+------+------+-------+-----------+------+-------+ | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 1 | 92 | | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 2 | 85 | | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 3 | 88 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 2 | 90 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 3 | 80 | | 201215128 | 陈冬 | 男 | 18 | IS | 201215128 | 1 | 78 | | 201215123 | 王敏 | 女 | 18 | MA | NULL | NULL | NULL | | 201215125 | 张立 | 男 | 19 | IS | NULL | NULL | NULL | | 201215126 | 张成民 | 男 | 18 | CS | NULL | NULL | NULL | +-----------+-----------+------+------+-------+-----------+------+-------+
In diesem Beispiel Der linke Join basiert grundsätzlich auf den Datensätzen in der Studententabelle. Die Studententabelle kann als linke Tabelle und die sc-Tabelle als rechte Tabelle betrachtet werden. Die Datensätze in der linken Tabelle werden vollständig angezeigt. plus die übereinstimmende rechte Tabelle. Wenn es auf der linken Seite keine Übereinstimmung gibt, wird der Rest als Null angezeigt.
Die using-Klausel ähnelt der on-Klausel, die Ergebnisse unterscheiden sich jedoch geringfügig.
Zum Beispiel:
mysql> select student.Sno,Sname,Grade from student left join sc on student.Sno=sc.Sno; +-----------+-----------+-------+| Sno | Sname | Grade | +-----------+-----------+-------+| 201215121 | 李勇 | 92 | | 201215121 | 李勇 | 85 | | 201215121 | 李勇 | 88 | | 201215122 | 刘晨 | 90 | | 201215122 | 刘晨 | 80 | | 201215128 | 陈冬 | 78 | | 201215123 | 王敏 | NULL | | 201215125 | 张立 | NULL || 201215126 | 张成民 | NULL | +-----------+-----------+-------+9 rows in set (0.00 sec)
Das Obige entspricht
select Sno,Sname,Grade from student left join sc using(Sno); +-----------+-----------+-------+| Sno | Sname | Grade | +-----------+-----------+-------+| 201215121 | 李勇 | 92 | | 201215121 | 李勇 | 85 | | 201215121 | 李勇 | 88 | | 201215122 | 刘晨 | 90 | | 201215122 | 刘晨 | 80 | | 201215128 | 陈冬 | 78 | | 201215123 | 王敏 | NULL | | 201215125 | 张立 | NULL || 201215126 | 张成民 | NULL | +-----------+-----------+-------+
an verschiedenen Stellen, zum Beispiel:
select * from student left join sc on student.Sno=sc.Sno; +-----------+-----------+------+------+-------+-----------+------+-------+ | Sno | Sname | Ssex | Sage | Sdept | Sno | Cno | Grade | +-----------+-----------+------+------+-------+-----------+------+-------+ | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 1 | 92 | | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 2 | 85 | | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 3 | 88 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 2 | 90 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 3 | 80 | | 201215128 | 陈冬 | 男 | 18 | IS | 201215128 | 1 | 78 | | 201215123 | 王敏 | 女 | 18 | MA | NULL | NULL | NULL | | 201215125 | 张立 | 男 | 19 | IS | NULL | NULL | NULL | | 201215126 | 张成民 | 男 | 18 | CS | NULL | NULL | NULL | +-----------+-----------+------+------+-------+-----------+------+-------+
select * from student left join sc using (sno); +-----------+-----------+------+------+-------+------+-------+ | Sno | Sname | Ssex | Sage | Sdept | Cno | Grade | +-----------+-----------+------+------+-------+------+-------+ | 201215121 | 李勇 | 男 | 22 | CS | 1 | 92 | | 201215121 | 李勇 | 男 | 22 | CS | 2 | 85 | | 201215121 | 李勇 | 男 | 22 | CS | 3 | 88 | | 201215122 | 刘晨 | 女 | 19 | CS | 2 | 90 | | 201215122 | 刘晨 | 女 | 19 | CS | 3 | 80 | | 201215128 | 陈冬 | 男 | 18 | IS | 1 | 78 | | 201215123 | 王敏 | 女 | 18 | MA | NULL | NULL | | 201215125 | 张立 | 男 | 19 | IS | NULL | NULL | | 201215126 | 张成民 | 男 | 18 | CS | NULL | NULL | +-----------+-----------+------+------+-------+------+-------+
Sno duplizieren Spalte. Wenn das On-Wort verwendet wird, wird es zweimal ausgegeben.
ist dasselbe wie LEFT JOIN, basiert jedoch auf der rechten Tabelle, zum Beispiel:
select * from student right join sc using (sno); +-----------+------+-------+--------+------+------+-------+ | Sno | Cno | Grade | Sname | Ssex | Sage | Sdept | +-----------+------+-------+--------+------+------+-------+ | 201215121 | 1 | 92 | 李勇 | 男 | 22 | CS | | 201215121 | 2 | 85 | 李勇 | 男 | 22 | CS | | 201215121 | 3 | 88 | 李勇 | 男 | 22 | CS | | 201215122 | 2 | 90 | 刘晨 | 女 | 19 | CS | | 201215122 | 3 | 80 | 刘晨 | 女 | 19 | CS | | 201215128 | 1 | 78 | 陈冬 | 男 | 18 | IS | +-----------+------+-------+--------+------+------+-------+
zeigt nicht an, wer darauf basiert, sondern nur die Datensätze, die die Bedingungen erfüllen angezeigt
select * from student inner join sc on student.Sno=sc.Sno; +-----------+--------+------+------+-------+-----------+------+-------+ | Sno | Sname | Ssex | Sage | Sdept | Sno | Cno | Grade | +-----------+--------+------+------+-------+-----------+------+-------+ | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 1 | 92 | | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 2 | 85 | | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 3 | 88 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 2 | 90 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 3 | 80 | | 201215128 | 陈冬 | 男 | 18 | IS | 201215128 | 1 | 78 | +-----------+--------+------+------+-------+-----------+------+-------+
Die obige Anweisung entspricht:
select * from student,sc where student.Sno=sc.Sno; +-----------+--------+------+------+-------+-----------+------+-------+ | Sno | Sname | Ssex | Sage | Sdept | Sno | Cno | Grade | +-----------+--------+------+------+-------+-----------+------+-------+ | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 1 | 92 | | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 2 | 85 | | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 3 | 88 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 2 | 90 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 3 | 80 | | 201215128 | 陈冬 | 男 | 18 | IS | 201215128 | 1 | 78 | +-----------+--------+------+------+-------+-----------+------+-------+
Sie können eine Where-Anweisung
select * from student left join sc using(Sno) where sc.Sno is null; +-----------+-----------+------+------+-------+------+-------+ | Sno | Sname | Ssex | Sage | Sdept | Cno | Grade | +-----------+-----------+------+------+-------+------+-------+ | 201215123 | 王敏 | 女 | 18 | MA | NULL | NULL | | 201215125 | 张立 | 男 | 19 | IS | NULL | NULL | | 201215126 | 张成民 | 男 | 18 | CS | NULL | NULL | +-----------+-----------+------+------+-------+------+-------+
kann mit Union-Klauseln kombiniert werden, da in diesem Beispiel die richtigen Alle übereinstimmen, sodass das angezeigte Ergebnis mit dem vorherigen übereinstimmt.
select * from student left join sc using(Sno) where student.Sno is null union select * from student left join sc using(Sno) where sc.Sno is null; +-----------+-----------+------+------+-------+------+-------+ | Sno | Sname | Ssex | Sage | Sdept | Cno | Grade | +-----------+-----------+------+------+-------+------+-------+ | 201215123 | 王敏 | 女 | 18 | MA | NULL | NULL | | 201215125 | 张立 | 男 | 19 | IS | NULL | NULL | | 201215126 | 张成民 | 男 | 18 | CS | NULL | NULL | +-----------+-----------+------+------+-------+------+-------+
select * from student left join sc on student.Sno=sc.Sno union select * from student right join sc on student.Sno=sc.Sno; +-----------+-----------+------+------+-------+-----------+------+-------+ | Sno | Sname | Ssex | Sage | Sdept | Sno | Cno | Grade | +-----------+-----------+------+------+-------+-----------+------+-------+ | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 1 | 92 | | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 2 | 85 | | 201215121 | 李勇 | 男 | 22 | CS | 201215121 | 3 | 88 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 2 | 90 | | 201215122 | 刘晨 | 女 | 19 | CS | 201215122 | 3 | 80 | | 201215128 | 陈冬 | 男 | 18 | IS | 201215128 | 1 | 78 | | 201215123 | 王敏 | 女 | 18 | MA | NULL | NULL | NULL | | 201215125 | 张立 | 男 | 19 | IS | NULL | NULL | NULL | | 201215126 | 张成民 | 男 | 18 | CS | NULL | NULL | NULL | +-----------+-----------+------+------+-------+-----------+------+-------+
Hinweis: Ein Links-Join B entspricht dem B-Rechts-Join A
mysql> select * from student left join sc using(Sno); +-----------+-----------+------+------+-------+------+-------+ | Sno | Sname | Ssex | Sage | Sdept | Cno | Grade | +-----------+-----------+------+------+-------+------+-------+ | 201215121 | 李勇 | 男 | 22 | CS | 1 | 92 | | 201215121 | 李勇 | 男 | 22 | CS | 2 | 85 | | 201215121 | 李勇 | 男 | 22 | CS | 3 | 88 | | 201215122 | 刘晨 | 女 | 19 | CS | 2 | 90 | | 201215122 | 刘晨 | 女 | 19 | CS | 3 | 80 | | 201215128 | 陈冬 | 男 | 18 | IS | 1 | 78 | | 201215123 | 王敏 | 女 | 18 | MA | NULL | NULL | | 201215125 | 张立 | 男 | 19 | IS | NULL | NULL | | 201215126 | 张成民 | 男 | 18 | CS | NULL | NULL | +-----------+-----------+------+------+-------+------+-------+ 9 rows in set (0.00 sec)mysql> select * from sc right join student using(Sno); +-----------+-----------+------+------+-------+------+-------+ | Sno | Sname | Ssex | Sage | Sdept | Cno | Grade | +-----------+-----------+------+------+-------+------+-------+ | 201215121 | 李勇 | 男 | 22 | CS | 1 | 92 | | 201215121 | 李勇 | 男 | 22 | CS | 2 | 85 | | 201215121 | 李勇 | 男 | 22 | CS | 3 | 88 | | 201215122 | 刘晨 | 女 | 19 | CS | 2 | 90 | | 201215122 | 刘晨 | 女 | 19 | CS | 3 | 80 | | 201215128 | 陈冬 | 男 | 18 | IS | 1 | 78 | | 201215123 | 王敏 | 女 | 18 | MA | NULL | NULL | | 201215125 | 张立 | 男 | 19 | IS | NULL | NULL | | 201215126 | 张成民 | 男 | 18 | CS | NULL | NULL | +-----------+-----------+------+------+-------+------+-------+
Das Obige ist die Analyse der Join-Nutzung in MySQL. Weitere verwandte Inhalte finden Sie auf der chinesischen PHP-Website (www.php.cn)!