MySQL--连接查询

Author:Allen_Huang

Version:1.0.0

一、内连接查询 inner join
关键字:inner join on

语句:select * from a_table a inner join b_table b on a.a_id = b.b_id;

采用内连接查询方式:

SELECT boy.hid,boy.bname,girl.gname FROM boy INNER JOIN girl ON girl.hid = boy.hid;

二、左连接查询 left join
关键字:left join on / left outer join on

语句:SELECT * FROM a_table a left join b_table b ON a.a_id = b.b_id;

说明: left join 是left outer join的简写,它的全称是左外连接,是外连接中的一种。 左(外)连接,左表(a_table)的记录将会全部表示出来,而右表(b_table)只会显示符合搜索条件的记录。右表记录不足的地方均为NULL。

采用内连接查询方式:

SELECT boy.hid,boy.bname,girl.gname FROM boy LEFT JOIN girl ON girl.hid = boy.hid;

三、右连接 right join
关键字:right join on / right outer join on

语句:SELECT * FROM a_table a right outer join b_table b on a.a_id = b.b_id;

说明:right join是right outer join的简写,它的全称是右外连接,是外连接中的一种。与左(外)连接相反,右(外)连接,左表(a_table)只会显示符合搜索条件的记录,而右表(b_table)的记录将会全部表示出来。左表记录不足的地方均为NULL。

采用内连接查询方式:

SELECT boy.hid,boy.bname,girl.gname FROM boy RIGHT JOIN girl ON girl.hid = boy.hid;

四、全连接 union
关键字:union /union all

语句:(select colum1,colum2…columN from tableA ) union (select colum1,colum2…columN from tableB )

或 (select colum1,colum2…columN from tableA ) union all (select colum1,colum2…columN from tableB );

union语句注意事项:

1.通过union连接的SQL它们分别单独取出的列数必须相同;

2.不要求合并的表列名称相同时,以第一个sql 表列名为准;

3.使用union 时,完全相等的行,将会被合并,由于合并比较耗时,一般不直接使用 union 进行合并,而是通常采用union all 进行合并;

4.被union 连接的sql 子句,单个子句中不用写order by ,因为不会有排序的效果。但可以对最终的结果集进行排序;

(select id,name from A order by id) union all (select id,name from B order by id); //没有排序效果

(select id,name from A ) union all (select id,name from B ) order by id; //有排序效果

采用 union 全连接:

union会自动将完全重复的数据去除掉;

采用 union all 全连接:

union all会保留那些重复的数据;

猜你喜欢

转载自blog.csdn.net/m0_46487331/article/details/112021694
今日推荐