您的位置
主页 > 网站技术 > 数据库 > » 正文

mysql的左右内连接用法实例

来源: 锦尚中国 点击:

本文实例讲述了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中的所有记录均会被列出。分以下三种情况:




首页  - 关于站长圈  - 广告服务  - 联系我们  - 关于站长圈  - 网站地图  - 版权声明