mysql的左右內串連用法執行個體_Mysql

來源:互聯網
上載者:User

本文執行個體講述了mysql的左右內串連用法。分享給大家供大家參考。具體如下:

用個例子來解析下mysql的左串連, 右串連和內串連

複製代碼 代碼如下:
create table user_id ( id decimal(18) );
create table user_profile ( id decimal(18) , name varchar(255) ) ;
insert into user_id values (1);
insert into user_id values (2);
insert into user_id values (3);
insert into user_id values (4);
insert into user_id values (5);
insert into user_id values (6);
insert into user_id values (1);

insert into user_profile values (1, "aa");
insert into user_profile values (2, "bb");
insert into user_profile values (3, "cc");
insert into user_profile values (4, "dd");
insert into user_profile values (5, "ee");
insert into user_profile values (5, "EE");
insert into user_profile values (8, 'zz');

一. 左串連:

複製代碼 代碼如下:
mysql> select a.id id , ifnull(b.name, 'N/A') name from user_id a left join user_profile b on a.id = b.id;
mysql> select a.id id , ifnull(b.name, 'N/A') name from user_id a left join user_profile b on a.id = b.id;   
+------+------+
| id   | name |
+------+------+
|    1 | aa   |
|    2 | bb   |
|    3 | cc   |
|    4 | dd   |
|    5 | ee   |
|    5 | EE   |
|    6 | N/A  |
|    1 | aa   |
+------+------+
8 rows in set (0.00 sec)

user_id居左,故謂之左串連。 這種情況下,以user_id為主,即user_id中的所有記錄均會被列出。分以下三種情況:

1. 對於user_id中的每一條記錄對應的id如果在user_profile中也恰好存在而且剛好只有一條,那麼就會在返回的結果中形成一條新的記錄。如上面1, 2, 3, 4對應的情況。
2. 對於user_id中的每一條記錄對應的id如果在user_profile中也恰好存在而且有N條,那麼就會在返回的結果中形成N條新的記錄。如上面的5對應的情況。
3. 對於user_id中的每一條記錄對應的id如果在user_profile中不存在,那麼就會在返回的結果中形成一條條新的記錄,且該記錄的右邊全部NULL。如上面的6對應的情況。

不符合上面三條規則的記錄不會被列出。

比如, 要查詢在一個相關的表中不存在的資料, 通過id關聯,要查出user_id表中存在user_profile中不存在的記錄:

複製代碼 代碼如下:
select count(*) from user_id left join user_profile on user_id.id = user_profile.id where user_profile.id is null;

二. 右串連

user_profile居右,故謂之右串連。 這種情況下, 以user_profile為主,即user_profile的所有記錄均會被列出。分以下三種情況:

1. 對於user_profile中的每一條記錄對應的id如果在user_id中也恰好存在而且剛好只有一條,那麼就會在返回的結果中形成一條新的記錄。如上面2, 3, 4, 5對應的情況。
2. 對於user_profile中的每一條記錄對應的id如果在user_id中也恰好存在而且有N條,那麼就會在返回的結果中形成N條新的記錄。如上面的1對應的情況。
3. 對於user_profile中的每一條記錄對應的id如果user_id中不存在,那麼就會在返回的結果中形成一條條新的記錄,且該記錄的左邊全部NULL。如上面的8對應的情況。

不符合上面三條規則的記錄不會被列出。

三. 內串連

MySQL內串連的資料記錄中,不會存在欄位為NULL的情況。可以簡單地認為,內連結的結果就是在左串連或者右串連的結果中剔除存在欄位為NULL的記錄後所得到的結果, 另外,MySQL不支援full join

複製代碼 代碼如下:
mysql> select *  from user_id a inner join user_profile b on a.id = b.id;
+------+------+------+
| id   | id   | name |
+------+------+------+
|    1 |    1 | aa   |
|    1 |    1 | aa   |
|    2 |    2 | bb   |
|    3 |    3 | cc   |
|    4 |    4 | dd   |
|    5 |    5 | ee   |
|    5 |    5 | EE   |
+------+------+------+
7 rows in set (0.00 sec)
mysql> select * from user_id a, user_profile b where a.id = b.id;                   
+------+------+------+
| id   | id   | name |
+------+------+------+
|    1 |    1 | aa   |
|    1 |    1 | aa   |
|    2 |    2 | bb   |
|    3 |    3 | cc   |
|    4 |    4 | dd   |
|    5 |    5 | ee   |
|    5 |    5 | EE   |
+------+------+------+
7 rows in set (0.00 sec)
mysql> select *  from user_id a join user_profile b on a.id = b.id;    
+------+------+------+
| id   | id   | name |
+------+------+------+
|    1 |    1 | aa   |
|    1 |    1 | aa   |
|    2 |    2 | bb   |
|    3 |    3 | cc   |
|    4 |    4 | dd   |
|    5 |    5 | ee   |
|    5 |    5 | EE   |
+------+------+------+
7 rows in set (0.00 sec)

希望本文所述對大家的MySQL程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.