详解 left join

公式:

select t.* ,t1.* from t1 left join t2  on t1.id =t2.id   and t1.name='ss' and t2.name='cc' 

where t1.age >10 

注释:

1.  优先级 on > where :  就是说先执行on 在执行where

   where 是在on 的出的结果集以后才进行筛选的;

2.  on 是先筛选再关联:  

    例子中:on t1.id =t2.id   and t1.name='ss' and t2.name='cc' 

     on后面的 t1.name='ss' and t2.name='cc'  : 这种单表设定条件的叫做筛选,就先执行

   on  t1.id = t2.id : 这种设定两个表关系的叫做关联

----------------------

以下实例基于该表和数据:

CREATE TABLE `class` (

  `class_id` int NOT NULL,

  `class_name` varchar(100) DEFAULT NULL,

  `class_grade` varchar(100) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;


CREATE TABLE `score` (

  `class_id` int NOT NULL,

  `stu_id` varchar(100) DEFAULT NULL,

  `score` int DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;


INSERT INTO `mytest`.`class` (`class_id`, `class_name`, `class_grade`) VALUES ('1', '语文', 'A');

INSERT INTO `mytest`.`class` (`class_id`, `class_name`, `class_grade`) VALUES ('2', '数学', 'B');

INSERT INTO `mytest`.`class` (`class_id`, `class_name`, `class_grade`) VALUES ('3', '英语', 'C');


INSERT INTO `mytest`.`score` (`class_id`, `stu_id`, `score`) VALUES ('1', 'A001', '91');

INSERT INTO `mytest`.`score` (`class_id`, `stu_id`, `score`) VALUES ('2', 'A001', '95');

INSERT INTO `mytest`.`score` (`class_id`, `stu_id`, `score`) VALUES ('1', 'A002', '82');

INSERT INTO `mytest`.`score` (`class_id`, `stu_id`, `score`) VALUES ('2', 'A001', '87');

INSERT INTO `mytest`.`score` (`class_id`, `stu_id`, `score`) VALUES ('3', 'B003', '65');


-- 1 基础的

select * from class c left join score s  on c.class_id = s.class_id;

-- 2 右表score=90 不存在,那么左表相当于关联空表

select * from class c left join score s on c.class_id = s.class_id and s.score =90

-- 3

  select * from class c left join score s

on c.class_id = s.class_id and c.class_name='语文'  and s.score =90

-- 4  关联结果中 不存在c.class_name ='语文' and s.score='90'

  select * from class c left join score s on c.class_id = s.class_id

  where c.class_name ='语文' and s.score='90'

-- 5 1=0 关联失败只是现实左侧; 只是显示左侧,右侧为空

  select * from class c left join score s on c.class_id  = s.class_id and 1 =0

-- 6  只是显示左侧,右侧为空

  select * from class c left join score s on 1 =0

-- 7 注意 c.class_name='语文'

-- 是对左表加的条件,那么表仅仅使用让左表从c.class_name='语文' 的数据与右表关联

-- 虽然左表class_name=!'语文'也符合c.lass_id=s.class_id 但是由于左表加上了限定条件

-- 所以不允许关联,仅仅允许关联c.class_name='语文'的数据按照c.class_id = s.class_id 关系去关联

-- 但是左侧的数据未关联也要求全部显示

    select * from class c left join score s on c.class_id = s.class_id and c.class_name='语文'

-- 8 其实分析同第7条,他仅仅允许左侧clss_name='英语' 的数据和右侧关联,其他的哪怕能关联上都不允许关联,

-- 只是允许显示出来

select * from class c left join score s on c.class_id = s.class_id and c.class_name='英语'

-- 9 左侧不存在class_name='体育'的数据,那么仅仅显示出左侧所有数据,右侧空白

-- 就仅仅把握一个原则,不管左侧限定什么条件成立或则不成立,就仅仅允许条件成立的的数据去关联,其他的仅仅是显示不关联

select * from class c left join score s on c.class_id = s.class_id and c.class_name='体育'

-- 10 首先左侧数据全部显示,其次仅仅允许左侧class_name='语文'的去关联,右侧限定仅仅允许s.socre='91'的这一条数据关联

  select * from class c left join score s on c.class_id = s.class_id and c.class_name='语文' and s.score='91'

-- 11 首先左侧数据全部显示,但是不存在c.class_name='体育'的数据,所以就左侧的所有数据都不需要关联了,右侧数据

-- 也不存在,所以右侧全部为空

  select * from class c left join score s on c.class_id = s.class_id and c.class_name='体育' and s.score='90'

-- 12 首先左侧数据全部显示,但是不存在c.class_name='体育'的数据,所以就左侧的所有数据都不需要关联了,右侧数据

-- 虽然存在,但是左侧限定了仅仅允许左侧c.class_name='体育'的数据数据去关联右侧,其他的无需关联的,所以右侧空白

  select * from class c left join score s on c.class_id = s.class_id and c.class_name='体育' and s.score='91'

-- 13 无数据,条数0  因为where 是关联以后的结果以后在查询s.score='90' 数据不存在

select * from class c left join score s on c.class_id = s.class_id

  where s.score='90'

-- 14 3条,请注意它与13语句的区别,s.score='90' 放在on 后面,他仅仅筛选右侧的数据发现不存在,但是左侧依然全部显示

-- 只是关联不上而已

select * from class c left join score s on c.class_id = s.class_id  and  s.score='90'

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容