图片与最后一部分来自:https://blog.csdn.net/plg17/article/details/78758593
语句:
select 表1查询的字段,表2查询的字段 from 表1 inner join 表2 on 条件;
如:
mysql> select a.*,b.* from course as a inner join rollcall as b on a.course_id=b.course_id;
+-----------+-------------+------+---------+-----------+-----------+----+-----------+------------+--------------+--------------+---------+
| course_id | course_name | time | teacher | classroom | t_command | db | course_id | student_id | student_name | teacher_name | command |
+-----------+-------------+------+---------+-----------+-----------+----+-----------+------------+--------------+--------------+---------+
| 1 | 1234 | 123 | 123 | 123 | 123 | 2 | 1 | 1501010096 | 于宗云 | 123 | 123 |
+-----------+-------------+------+---------+-----------+-----------+----+-----------+------------+--------------+--------------+---------+
1 row in set (0.07 sec)
语句:
select 表1查询的字段,表2查询的字段 from 表1 left join 表2 on 条件; // 只改变了连接的语句,其他写法相同
如:
mysql> select a.*,b.* from course as a left join rollcall as b on a.course_id=b.course_id;
+-----------+-------------+------+---------+-----------+-----------+------+-----------+------------+--------------+--------------+---------+
| course_id | course_name | time | teacher | classroom | t_command | db | course_id | student_id | student_name | teacher_name | command |
+-----------+-------------+------+---------+-----------+-----------+------+-----------+------------+--------------+--------------+---------+
| 1 | 1234 | 123 | 123 | 123 | 123 | 2 | 1 | 1501010096 | 于宗云 | 123 | 123 |
| 2 | 123 | 123 | 123 | 123 | 123 | NULL | NULL | NULL | NULL | NULL | NULL |
+-----------+-------------+------+---------+-----------+-----------+------+-----------+------------+--------------+--------------+---------+
说明:
left join 是left outer join的简写,它的全称是左外连接,是外连接中的一种。
左(外)连接,左表(a_table)的记录将会全部表示出来,而右表(b_table)只会显示符合搜索条件的记录。右表记录不足的地方均为NULL。
语句:
select 表1查询的字段,表2查询的字段 from 表1 right join 表2 on 条件; // 只改变了连接的语句,其他写法相同
如:
mysql> select a.*,b.* from course as a right join rollcall as b on a.course_id=b.course_id;
+-----------+-------------+------+---------+-----------+-----------+----+-----------+------------+--------------+--------------+---------+
| course_id | course_name | time | teacher | classroom | t_command | db | course_id | student_id | student_name | teacher_name | command |
+-----------+-------------+------+---------+-----------+-----------+----+-----------+------------+--------------+--------------+---------+
| 1 | 1234 | 123 | 123 | 123 | 123 | 2 | 1 | 1501010096 | 于宗云 | 123 | 123 |
+-----------+-------------+------+---------+-----------+-----------+----+-----------+------------+--------------+--------------+---------+
1 row in set (0.00 sec)
说明:
right join是right outer join的简写,它的全称是右外连接,是外连接中的一种。
与左(外)连接相反,右(外)连接,左表(a_table)只会显示符合搜索条件的记录,而右表(b_table)的记录将会全部表示出来。左表记录不足的地方均为NULL。
MySQL 已经没有全连接了,有的教程上还写着 full join 但是实现不了,不过可以换一种方式来查询。
语句:
(SELECT * from a left JOIN b on a.name=b.id) UNION (SELECT * from a RIGHT JOIN b on a.name=b.id );
mysql> (select a.*,b.* from course as a right join rollcall as b on a.course_id=b.course_id) UNION ( select a.*,b.* from course as a LEFT join rollcall as b on a.course_id=b.course_id);
+-----------+-------------+------+---------+-----------+-----------+------+-----------+------------+--------------+--------------+---------+
| course_id | course_name | time | teacher | classroom | t_command | db | course_id | student_id | student_name | teacher_name | command |
+-----------+-------------+------+---------+-----------+-----------+------+-----------+------------+--------------+--------------+---------+
| 1 | 1234 | 123 | 123 | 123 | 123 | 2 | 1 | 1501010096 | 于宗云 | 123 | 123 |
| NULL | NULL | NULL | NULL | NULL | NULL | 3 | NULL | NULL | NULL | NULL | NULL |
| 2 | 123 | 123 | 123 | 123 | 123 | NULL | NULL | NULL | NULL | NULL | NULL |
| 3 | 123 | 123 | 123 | 123 | 123 | NULL | NULL | NULL | NULL | NULL | NULL |
+-----------+-------------+------+---------+-----------+-----------+------+-----------+------------+--------------+--------------+---------+
4 rows in set (0.00 sec)
**MySQL认为任何一个查询都是一次“关联”,**并不仅仅是一个查询需要到两个表匹配才叫关联,所以在MySQL中,每一个查询,每一个片段(包括子查询,甚至基于单表查询)都可以是一次关联。
当前MySQL关联执行的策略很简单:**MySQL对任何关联都执行嵌套循环关联操作,即MySQL先在一个表中循环取出单条数据,然后在嵌套循环到下一个表中寻找匹配的行,依次下去,直到找到所有表中匹配的行为止。**然后根据各个表匹配的行,返回查询中需要的各个列。请看下面的例子中的简单的查询:
查询语句:select tbl1.col1, tbl2.col2 from tbl1 inner join tbl2 using(col3) where tbl1.col1 in (5, 6);
假设MySQL按照查询中的表顺序进行关联操作,我们则可以用下面的伪代码表示MySQL将如何完成这个查询:
outer_iter = iterator over tbl1 where col1 in (5, 6)
outer_row = outer_iter.next
while outer_row
inner_iter = iterator over tbl2 where col3 = outer_row.col3
inner_row = inner_iter.next
while inner_row
output [ outer_row.col1, inner_row.col2]
inner_row = inner_iter.next
end
outer_row = outer_iter.next
end
上面的执行计划对于单表查询和多表关联查询都适用,如果是一个单表查询,那么只需要上面外层的基本操作。对于外连接,上面的执行过程仍然适用。例如,我们将上面的查询语句修改如下:
select tbl1.col1, tbl2.col2 from tbl1 left outer join tbl2 using(col3) where tbl1.col1 in (5, 6);
那么,对应的伪代码如下:
outer_iter = iterator over tbl1 where col1 in (5, 6)
outer_row = outer_iter.next
while outer_row
inner_iter = iterator over tbl2 where col3 = outer_row.col3
inner_row = inner_iter.next
if inner_row
while inner_row
output [ outer_row.col1, inner_row.col2]
inner_row = inner_iter.next
end
else
output [ outer_row.col1, null]
end
outer_row = outer_iter.next
end