mysql多種join MySQL的幾種Join

2021-10-19 03:13:35 字數 1444 閱讀 6177

/* 左表t1*/

drop table if exists t1;

create table t1 (id int not null,name varchar(20));

insert into t1 values (1,『t1a『);

insert into t1 values (2,『t1b『);

insert into t1 values (3,『t1c『);

insert into t1 values (4,『t1d『);

insert into t1 values (5,『t1f『);

/* 右表 t2*/

drop table if exists t1;

create table t2 (id int not null,name varchar(20));

insert into t2 values (2,『t2b『);

insert into t2 values (3,『t2c『);

insert into t2 values (4,『t2d『);

insert into t2 values (5,『t2f『);

insert into t2 values (6,『t2a『);

#兩表關聯,把左表的列和右表的列通過笛卡爾積的形式表達出來。

select * from t1 join t2

#兩表關聯,左表全部保留,右表關聯不上用null表示。

select * from t1 left join t2 on t1.id =t2.id

#右表全部保留,左表關聯不上的用null表示。

select * from t1 right join t2 on t1.id =t2.`id`

#兩表關聯,保留兩表中交集的記錄。

select * from t1 inner join t2 on t1.id=t2.`id`

#兩表關聯,查詢左表獨有的資料。

select * from t1 left join t2 on t1.id =t2.`id` where t2.id is null

#兩表關聯,查詢右表獨有的資料。

select * from t1 right join t2 on t1.id =t2.id where t1.id is null

#兩表關聯,查詢它們的所有記錄

select * from t1 right join t2 on t1.id =t2.`id`

union

select * from t1 left join t2 on t1.id =t2.id

#兩表關聯,取並集然後去交集。select * from t1 left join t2 on t1.id =t2.`id` where t2.id is nullunionselect * from t1 right join t2 on t1.id =t2.id where t1.id is null

mysql 多種索引定義

1.普通索引 create index indexname on mytable username length 如果是char,varchar型別,length可以小於字段實際長度 如果是blob和text型別,必須指定 length。2.修改表結構 新增索引 alter table tablen...

MySQL有多種儲存引擎

mysql有多種儲存引擎 myisam innodb merge memory heap bdb berkeleydb example federated archive csv blackhole。mysql支援數個儲存引擎作為對不同表的型別的處理器。mysql儲存引擎包括處理事務安全表的引擎和處...

Mysql系列 Join多種用法

sql優化中常用的方法之一就是將表關聯或子查詢改為join的用法,如上圖所示join的用法有很多種,導致有很多小夥伴經常搞混。本文將通過具體例子介紹sql中的各種常用join的特性和使用。準備資料 create table tb1 ept id int 11 not null auto increm...