MySQL内连接、外连接、全连接
已有如下表
rollcall 数据表
course 数据表
内链接 inner join
语句:
select 表1查询的字段,表2查询的字段 from 表1 inner join 表2 on 条件;
如:
mysql> select a.*,b.* from course as a inner join rollcall as b urse_urse_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)
内连接会返回两表的交集:
外连接分为左外连接,右外连接
左外连接 left join
语句:
select 表1查询的字段,表2查询的字段 from 表1 left join 表2 on 条件; // 只改变了连接的语句,其他写法相同
如:
mysql> select a.*,b.* from course as a left join rollcall as b urse_urse_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。
右外连接 right join
语句:
select 表1查询的字段,表2查询的字段 from 表1 right join 表2 on 条件; // 只改变了连接的语句,其他写法相同
如:
mysql> select a.*,b.* from course as a right join rollcall as b urse_urse_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 ) UNION (SELECT * from a RIGHT JOIN b on );
mysql> (select a.*,b.* from course as a right join rollcall as b urse_urse_id) UNION ( select a.*,b.* from course as a LEFT join rollcall as b urse_urse_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对任何关联都执⾏嵌套循环关联操作,即MySQL先在
⼀个表中循环取出单条数据,然后在嵌套循环到下⼀个表中寻匹配的⾏,依次下去,直到到所有表中匹配的⾏为⽌。**然后根据各个表匹配的⾏,返回查询中需要的各个列。请看下⾯的例⼦中的简单的查询:
查询语句:l1, l2 from tbl1 inner join tbl2 using(col3) l1 in (5, 6);
假设MySQL按照查询中的表顺序进⾏关联操作,我们则可以⽤下⾯的伪代码表⽰MySQL将如何完成这个查询:
outer_iter = iterator over tbl1 where col1 in (5, 6)
outer_row =
while outer_row
inner_iter = iterator over tbl2 where col3 = l3
inner_row =
while inner_row
output [ l1, l2]
inner_row =
end
outer_row =
end
上⾯的执⾏计划对于单表查询和多表关联查询都适⽤,如果是⼀个单表查询,那么只需要上⾯外层的基本操作。对于外连接,上⾯的执⾏过程仍然适⽤。例如,我们将上⾯的查询语句修改如下:
l1, l2 from tbl1 left outer join tbl2 using(col3) l1 in (5, 6);
那么,对应的伪代码如下:
outer_iter = iterator over tbl1 where col1 in (5, 6)
outer_row =
while outer_row
inner_iter = iterator over tbl2 where col3 = l3
inner_row =
if inner_row
while inner_row
output [ l1, l2]
inner_row =
end
else
output [ l1, null]
end
outer_row =
mysql下载链接end
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论