mysql左连接,右连接,全连接,公共连接

一、建表
create table Guoji_A (
id bigint primary key AUTO_INCREMENT,
name varchar(20) ,
tel bigint
)

create table Guoji_B (
id bigint primary key AUTO_INCREMENT,
name varchar(20) ,
address bigint
)

二、插数据
insert into Guoji_A values (null,“大娃”,186),(null,“二娃”,187),(null,“三娃”,188),(null,“五娃”,189);

insert into Guoji_B values (null,“二娃”,10002),(null,“三娃”,10003),(null,“四娃”,10004);

三、表关联

select * from Guoji_A a left join Guoji_B b on a.name = b.name;
在这里插入图片描述
select * from Guoji_A a right join Guoji_B b on a.name = b.name;
在这里插入图片描述
☆☆☆☆☆☆mysql不支持全连接 用左连接联合右连接
select * from Guoji_A a LEFT JOIN Guoji_B b on a.name = b.name

UNION select * from Guoji_A a RIGHT JOIN Guoji_B b on a.name = b.name;
在这里插入图片描述
select * from Guoji_A a join Guoji_B b on a.name = b.name;
在这里插入图片描述

总结:
1、建表时 varchar 需要写字段最大长度;
2、插入数据时 主键id自增 ,只需写null
3、左连接,左表全有,右边缺的字段补null
右连接:右表全有,左边缺的字段补null
全连接(mysql不支持,可以通过union):左右表全有,缺字段补null
交集:join 左右表共有

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章