MySQL 连接查询 学习笔记

#交叉连接(返回笛卡尔交集)
AXB={(a,b)|a属于A且b属于B },则AXB所形成的集合就叫笛卡尔集

1 SELECT * from employees cross join depts;
2 SELECT * from employees,depts;

 

#内连接(只返回满足连接条件的记录,不返回不满足连接条件的记录)

1 select * from employees t1 INNER join depts t2 
2 on t1.dept_id = t2.dept_id;

 

#右外连接(不仅返回满足连接条件的记录,不满足连接条件的记录也返回,但是不返回从表里不满足连接条件的记录)

1 select * from employees t1 RIGHT join depts t2 
2 on t1.dept_id = t2.dept_id;

 

#左外连接(不仅返回满足连接条件的记录,不满足连接条件的记录也返回,但是不返回从表里不满足连接条件的记录)

select * from employees t1 LEFT join depts t2 
on t1.dept_id = t2.dept_id;

 

SELECT t1.name '员工姓名', t2.name '所在部门' from employees t1

1 left join depts t2 on t1.dept_id = t2.dept_id;

 

posted @ 2018-05-09 17:54  没梦想做什么咸鱼  阅读(126)  评论(0)    收藏  举报