最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

mysql的左右內(nèi)連接用法實例

 更新時間:2015年02月09日 16:42:27   作者:另一花生  
這篇文章主要介紹了mysql的左右內(nèi)連接用法,以一個完整實例較為詳細的分析了mysql的左右內(nèi)連接使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了mysql的左右內(nèi)連接用法。分享給大家供大家參考。具體如下:

用個例子來解析下mysql的左連接, 右連接和內(nèi)連接

復(fù)制代碼 代碼如下:
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');

一. 左連接:

復(fù)制代碼 代碼如下:
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中的每一條記錄對應(yīng)的id如果在user_profile中也恰好存在而且剛好只有一條,那么就會在返回的結(jié)果中形成一條新的記錄。如上面1, 2, 3, 4對應(yīng)的情況。
2. 對于user_id中的每一條記錄對應(yīng)的id如果在user_profile中也恰好存在而且有N條,那么就會在返回的結(jié)果中形成N條新的記錄。如上面的5對應(yīng)的情況。
3. 對于user_id中的每一條記錄對應(yīng)的id如果在user_profile中不存在,那么就會在返回的結(jié)果中形成一條條新的記錄,且該記錄的右邊全部NULL。如上面的6對應(yīng)的情況。

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

比如, 要查詢在一個相關(guān)的表中不存在的數(shù)據(jù), 通過id關(guān)聯(lián),要查出user_id表中存在user_profile中不存在的記錄:

復(fù)制代碼 代碼如下:
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中的每一條記錄對應(yīng)的id如果在user_id中也恰好存在而且剛好只有一條,那么就會在返回的結(jié)果中形成一條新的記錄。如上面2, 3, 4, 5對應(yīng)的情況。
2. 對于user_profile中的每一條記錄對應(yīng)的id如果在user_id中也恰好存在而且有N條,那么就會在返回的結(jié)果中形成N條新的記錄。如上面的1對應(yīng)的情況。
3. 對于user_profile中的每一條記錄對應(yīng)的id如果user_id中不存在,那么就會在返回的結(jié)果中形成一條條新的記錄,且該記錄的左邊全部NULL。如上面的8對應(yīng)的情況。

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

三. 內(nèi)連接

MySQL內(nèi)連接的數(shù)據(jù)記錄中,不會存在字段為NULL的情況??梢院唵蔚卣J為,內(nèi)鏈接的結(jié)果就是在左連接或者右連接的結(jié)果中剔除存在字段為NULL的記錄后所得到的結(jié)果, 另外,MySQL不支持full join

復(fù)制代碼 代碼如下:
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程序設(shè)計有所幫助。

相關(guān)文章

最新評論

上饶市| 普宁市| 庆安县| 务川| 高邮市| 五大连池市| 雅安市| 夏河县| 南通市| 松桃| 乌兰察布市| 越西县| 东阳市| 介休市| 西藏| 桂林市| 许昌县| 鹰潭市| 鸡东县| 同江市| 龙州县| 菏泽市| 馆陶县| 凭祥市| 宜兰市| 苏尼特左旗| 兴化市| 阜南县| 讷河市| 通州区| 方正县| 商都县| 镇坪县| 盈江县| 六枝特区| 德钦县| 咸丰县| 洱源县| 当涂县| 嘉义县| 金寨县|