mysql连接查询【内连接,外连接】

首先先准备数据如下:
建立了一个person表和card表,并添加数据:


create database testjion;

create table person(
    id int,
    name varchar(20),
    cardid int
) character set = utf8;


create table card(
    id int,
    name varchar(20)
) character set = utf8;


insert into card values(1,'饭卡');
insert into card values(2,'建行卡');
insert into card values(3,'农行卡');
insert into card values(4,'工商卡'); 
insert into card values(5,'邮政卡');



insert into person values(1,'张三',1);
insert into person values(2,'李四',3);
insert into person values(3,'王五',6);
内连接

内连接:指连接结果仅包含符合连接条件的行,参与连接的两个表都应该符合连接条件。
如果数据表之前用外键进行关联的话,用where也是可以的,内连接可以在没有外键的情况下对连个数据表之间进行数据查询。
例如:

select * from card inner join person on card.id=person.cardid;

查询结果:

mysql> select *
    -> from card
    -> inner join person
    -> on card.id=person.cardid;
+------+--------+------+------+--------+
| id   | name   | id   | name | cardid |
+------+--------+------+------+--------+
|    1 | 饭卡   |    1 | 张三 |      1 |
|    3 | 农行卡 |    2 | 李四 |      3 |
+------+--------+------+------+--------+

就是以card中的id字段和person中的cardid字段进行筛选,字段相同的取交集。

外连接

外连接中又分左外连接,右外连接和全外连接

左外连接

左外连接会把左边的表中的数据全部取出,右边的表中如果有相等的数据就取出,没有的话用NULL补上。
看一个例子:

select * from person left join card on person.cardid=card.id;

看一下结果:

mysql> select * from person left join card on person.cardid=card.id;
+------+------+--------+------+--------+
| id   | name | cardid | id   | name   |
+------+------+--------+------+--------+
|    1 | 张三 |      1 |    1 | 饭卡   |
|    2 | 李四 |      3 |    3 | 农行卡 |
|    3 | 王五 |      6 | NULL | NULL   |
+------+------+--------+------+--------+

因为王五的卡号不在card中,所以用NULL补上

右外连接

右外里连接和左外连接是相反的:
把右边的表中的数据全部取出,左边的表中如果有相等的数据就取出,没有的话用NULL补上。

select * from person right join card on person.cardid=card.id;

查询结果是:

mysql> select * from person right join card on person.cardid=card.id;
+------+------+--------+------+--------+
| id   | name | cardid | id   | name   |
+------+------+--------+------+--------+
|    1 | 张三 |      1 |    1 | 饭卡   |
|    2 | 李四 |      3 |    3 | 农行卡 |
| NULL | NULL |   NULL |    2 | 建行卡 |
| NULL | NULL |   NULL |    4 | 工商卡 |
| NULL | NULL |   NULL |    5 | 邮政卡 |
+------+------+--------+------+--------+
全外连接

全外连接是左外连接和右外连接union一下的结果,mysql是不支持全外连接的,所以用union来实现

full joinsql语句的结果:
mysql> select * from person full join card on person.cardid=card.id;
ERROR 1054 (42S22): Unknown column 'person.cardid' in 'on clause'

用union实现:

mysql> select * from person left join card on person.cardid=card.id
    -> union
    -> select * from person right join card on person.cardid=card.id;
+------+------+--------+------+--------+
| id   | name | cardid | id   | name   |
+------+------+--------+------+--------+
|    1 | 张三 |      1 |    1 | 饭卡   |
|    2 | 李四 |      3 |    3 | 农行卡 |
|    3 | 王五 |      6 | NULL | NULL   |
| NULL | NULL |   NULL |    2 | 建行卡 |
| NULL | NULL |   NULL |    4 | 工商卡 |
| NULL | NULL |   NULL |    5 | 邮政卡 |
+------+------+--------+------+--------+
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值