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 左右表共有

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