测试两张表结构:
create table px_data_test_temp.test_score(
id int comment '学生id',
grade int comment '学生成绩'
) comment '学生成绩表';
create table px_data_test_temp.test_stu(
id int comment '学生id',
name string comment '学生姓名'
) comment '学生信息表';
插入测试数据:
insert into table px_data_test_temp.test_score
(id,grade) values
(1, 78),
(2, 90),
(3, 80),
(4, 70),
(5, 85);
insert into table px_data_test_temp.test_stu
(id,name) values
(1, "xiaoming"),
(2, "xiaohei"),
(3, "xiaohong"),
(4, "xiaobai"),
(5, "xiaolv");
条件放到on上
select a.id, name, grade
from
px_data_test_temp.test_score a
left join
px_data_test_temp.test_stu b
on a.id = b.id and a.grade > 80;
select a.id, name, grade
from
px_data_test_temp.test_score a
left join
px_data_test_temp.test_stu b
on a.id = b.id
where a.grade > 80;
或者(这种方式会产生笛卡尔积之后,再进行筛选)
select a.id, name, grade
from
px_data_test_temp.test_score a
left join
px_data_test_temp.test_stu b
where a.id = b.id and a.grade > 80;
结果:
区别:
on 和 where 在筛选条件的时候,on 会显示所有满足 | 不满足条件的数据而 where 只显示满足条件的数据。
条件放到on上
select a.id, name, grade
from
px_data_test_temp.test_score a
right join
px_data_test_temp.test_stu b
on a.id = b.id and a.grade > 80;
select a.id, name, grade
from
px_data_test_temp.test_score a
right join
px_data_test_temp.test_stu b
on a.id = b.id
where a.grade > 80;
或者(这种方式会产生笛卡尔积之后,再进行筛选)
select a.id, name, grade
from
px_data_test_temp.test_score a
right join
px_data_test_temp.test_stu b
where a.id = b.id and a.grade > 80;
条件放到on上
select a.id, name, grade
from
px_data_test_temp.test_score a
join
px_data_test_temp.test_stu b
on a.id = b.id and a.grade > 80;
select a.id, name, grade
from
px_data_test_temp.test_score a
join
px_data_test_temp.test_stu b
on a.id = b.id
where a.grade > 80;
或者(这种方式会产生笛卡尔积之后,再进行筛选)
select a.id, name, grade
from
px_data_test_temp.test_score a
join
px_data_test_temp.test_stu b
where a.id = b.id and a.grade > 80;
条件放到on上
select a.id, name, grade
from
px_data_test_temp.test_score a
full join
px_data_test_temp.test_stu b
on a.id = b.id and a.grade > 80;
select a.id, name, grade
from
px_data_test_temp.test_score a
full join
px_data_test_temp.test_stu b
on a.id = b.id
where a.grade > 80;
或者(这种方式会产生笛卡尔积之后,再进行筛选)
select a.id, name, grade
from
px_data_test_temp.test_score a
full join
px_data_test_temp.test_stu b
where a.id = b.id and a.grade > 80;
结果:
区别:
on 和 where 在筛选条件的时候,on 会显示所有满足 | 不满足条件的数据而 where 只显示满足条件的数据。