mysql四种连接查询

SQL 的四种连接查询

内连接
inner join 或者 join

外连接
1.左连接 left join 或者 left outer join

2.右连接 right join 或者 right outer join

3.完全外连接 full join 或者 full outer join

==创建一个数据库
create database testJoin;

==person 表
create table person(
id int,
name varchar(20),
cardId int);

==card 表
create table card(
id int,
name varchar(20));

#往person表添加数据
insert into person values(1,‘张三’,1);
insert into person values(2,‘李四’,3);
insert into person values(3,‘王五’,6);

mysql> select * from person;
±—±-----±-------+
| id | name | cardId |
±—±-----±-------+
| 1 | 张三 | 1 |
| 2 | 李四 | 3 |
| 3 | 王五 | 6 |
±—±-----±-------+
3 rows in set

#往card表添加数据
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,‘邮政卡’);

mysql> select * from card;
±—±-------+
| id | name |
±—±-------+
| 1 | 饭卡 |
| 2 | 建行卡 |
| 3 | 农行卡 |
| 4 | 工商卡 |
| 5 | 邮政卡 |
±—±-------+
5 rows in set

==并没有创建外键。

==1. inner join 查询 (内连接)
==内联查询,其实就是俩张表中的数据,通过某字段相等,查询出相关记录数据。

将person表与card表进行连接,用 on 表示条件
mysql> select * from person inner join card on person.cardId=card.id;
±—±-----±-------±—±-------+
| id | name | cardId | id | name |
±—±-----±-------±—±-------+
| 1 | 张三 | 1 | 1 | 饭卡 |
| 2 | 李四 | 3 | 3 | 农行卡 |
±—±-----±-------±—±-------+
2 rows in set

==inner 可以省略
mysql> select * from person join card on person.cardId=card.id;
±—±-----±-------±—±-------+
| id | name | cardId | id | name |
±—±-----±-------±—±-------+
| 1 | 张三 | 1 | 1 | 饭卡 |
| 2 | 李四 | 3 | 3 | 农行卡 |
±—±-----±-------±—±-------+
2 rows in set

==2.left join(左外连接)
==左外连接,会把左边的表里面的所有数据取出来,而右边表中的数据,如果有相等的,就显示出来,如果没有,就补上 NULL

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 |
±—±-----±-------±-----±-------+
3 rows in set

== left outer join 与 left join 一样
mysql> select * from person left outer join card on person.cardId=card.id;
±—±-----±-------±-----±-------+
| id | name | cardId | id | name |
±—±-----±-------±-----±-------+
| 1 | 张三 | 1 | 1 | 饭卡 |
| 2 | 李四 | 3 | 3 | 农行卡 |
| 3 | 王五 | 6 | NULL | NULL |
±—±-----±-------±-----±-------+
3 rows in set

==3.right join(右外连接)
==右外连接,会把右边的表里面的所有数据取出来,而左边表中的数据,如果有相等的,就显示出来,如果没有,就补上 NULL
mysql> select * from person right join card on person.cardId=card.id;
±-----±-----±-------±—±-------+
| id | name | cardId | id | name |
±-----±-----±-------±—±-------+
| 1 | 张三 | 1 | 1 | 饭卡 |
| NULL | NULL | NULL | 2 | 建行卡 |
| 2 | 李四 | 3 | 3 | 农行卡 |
| NULL | NULL | NULL | 4 | 工商卡 |
| NULL | NULL | NULL | 5 | 邮政卡 |
±-----±-----±-------±—±-------+
5 rows in set

== right outer join 与 right join 一样
mysql> select * from person right outer join card on person.cardId=card.id;
±-----±-----±-------±—±-------+
| id | name | cardId | id | name |
±-----±-----±-------±—±-------+
| 1 | 张三 | 1 | 1 | 饭卡 |
| NULL | NULL | NULL | 2 | 建行卡 |
| 2 | 李四 | 3 | 3 | 农行卡 |
| NULL | NULL | NULL | 4 | 工商卡 |
| NULL | NULL | NULL | 5 | 邮政卡 |
±-----±-----±-------±—±-------+
5 rows in set

==4. full join (全外连接)
==左边表和右边表并起来,没有的地方用 NULL 补上
mysql> select * from person full join card on person.cardId=card.id;
1054 - Unknown column ‘person.cardId’ in ‘on clause’

==mysql 不支持 full join
在这里插入图片描述

如果非要表示 full join 的话

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 | 邮政卡 |
±-----±-----±-------±-----±-------+
6 rows in set

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值