自串連、外串連和自串連查詢

來源:互聯網
上載者:User

表1:teacher
+----+--------+
| id | name   |
+----+--------+
|  1 | 劉德華 |
|  2 | 張學友 |
|  4 | 黎明   |
+----+--------+
表2:student
+----+------+--------+
| id | name | tea_id |
+----+------+--------+
|  1 | 張三 |      1 |
|  2 | 李四 |      1 |
|  3 | 王五 |      1 |
|  4 | 趙六 |      2 |
|  5 | 孫七 |      2 |
+----+------+--------+

1.內串連:在每個表中找出合格共有記錄。[x inner join y on...]
第一種寫法:(相當於多表查詢,用的是where!)
select t.*,s.* from teacher t,student s where t.id=s.tea_id;
第二種寫法:(inner省略了)
select t.*,s.* from teacher t join student s on t.id=s.tea_id;
第三種寫法:
select t.*,s.* from teacher t inner join student s on t.id=s.tea_id;

+----+--------+----+------+--------+
| id | name   | id | name | tea_id |
+----+--------+----+------+--------+
|  1 | 劉德華 |  1 | 張三 |      1 |
|  1 | 劉德華 |  2 | 李四 |      1 |
|  1 | 劉德華 |  3 | 王五 |      1 |
|  2 | 張學友 |  4 | 趙六 |      2 |
|  2 | 張學友 |  5 | 孫七 |      2 |
+----+--------+----+------+--------+

2.外串連有三種方式:左串連,右串連和全串連。
2.1 左串連:根據左表的記錄,在被串連的右表中找出合格記錄與之匹配
找不到與左表匹配的,用null表示。[x left [outer] join y on...]
第一種寫法:
select t.*,s.* from teacher t left join student s on t.id=s.tea_id;
第二種寫法:
select t.*,s.* from teacher t left outer join student s on t.id=s.tea_id;

+----+--------+------+------+--------+
| id | name   | id   | name | tea_id |
+----+--------+------+------+--------+
|  1 | 劉德華 |    1 | 張三 |      1 |
|  1 | 劉德華 |    2 | 李四 |      1 |
|  1 | 劉德華 |    3 | 王五 |      1 |
|  2 | 張學友 |    4 | 趙六 |      2 |
|  2 | 張學友 |    5 | 孫七 |      2 |
|  4 | 黎明   | NULL | NULL |   NULL |
+----+--------+------+------+--------+

2.2 右串連:根據右表的記錄,在被串連的左表中找出合格記錄與之匹配,
找不到匹配的,用null填充。[x right [outer] join y on...]
第一種寫法:
select t.*,s.* from teacher t right join student s on t.id=s.tea_id;
第二種寫法:
select t.*,s.* from teacher t right outer join student s on t.id=s.tea_id;

+------+--------+----+------+--------+
| id   | name   | id | name | tea_id |
+------+--------+----+------+--------+
|    1 | 劉德華 |  1 | 張三 |      1 |
|    1 | 劉德華 |  2 | 李四 |      1 |
|    1 | 劉德華 |  3 | 王五 |      1 |
|    2 | 張學友 |  4 | 趙六 |      2 |
|    2 | 張學友 |  5 | 孫七 |      2 |
+------+--------+----+------+--------+

2.3 全串連:返回合格所有表的記錄,沒有與之匹配的,用null表示(結果是左串連和右串連的並集)
第一種寫法:
select t.*,s.* from teacher t full join student s on t.id=s.tea_id;
第二種寫法:
select t.*,s.* from teacher t full outer join student s on t.id=s.tea_id;

3 交叉串連(結果是笛卡爾積)
select t.*,s.* from teacher t cross join student s;
等效於:
select t.*,s.* from teacher t,student s;
+----+--------+----+------+--------+
| id | name   | id | name | tea_id |
+----+--------+----+------+--------+
|  1 | 劉德華 |  1 | 張三 |      1 |
|  2 | 張學友 |  1 | 張三 |      1 |
|  4 | 黎明   |  1 | 張三 |      1 |
|  1 | 劉德華 |  2 | 李四 |      1 |
|  2 | 張學友 |  2 | 李四 |      1 |
|  4 | 黎明   |  2 | 李四 |      1 |
|  1 | 劉德華 |  3 | 王五 |      1 |
|  2 | 張學友 |  3 | 王五 |      1 |
|  4 | 黎明   |  3 | 王五 |      1 |
|  1 | 劉德華 |  4 | 趙六 |      2 |
|  2 | 張學友 |  4 | 趙六 |      2 |
|  4 | 黎明   |  4 | 趙六 |      2 |
|  1 | 劉德華 |  5 | 孫七 |      2 |
|  2 | 張學友 |  5 | 孫七 |      2 |
|  4 | 黎明   |  5 | 孫七 |      2 |
+----+--------+----+------+--------+

5自串連:串連的表都是同一個表,同樣可以由內串連,外串連各種組合
方式,按實際應用去組合。
SELECT a.*,b.* FROM table_1 a,table_1 b WHERE a.[name]=b.[name] --串連的兩表是同一個表,別稱不一樣

SELECT a.*,b.* FROM table_1 a LEFT JOIN table_1 b ON a.[name]=b.[name] --左串連寫法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.