内连接(join)与半连接(semi join)的区别就是有没有根据匹配字段连接重复字段的数据(其作用几乎相同)

如下表的 dept的 id=1 有两个重复的id 部门编号字段

emp(雇员表)

id    dept_id    name

1         1            z s

___________________________________________________

dept(部门表)

id      dept_name

1            xx

1            oo

 

当部门表中有重复的部门 编号 时,

join 时 会 将两条重复数据都匹配连接

zs 1, xx部门 --继续匹配连接

zx 1, oo部门

半连接, 在匹配了第一条数据后就停止匹配,不向下继续匹配, 只生成一条数据

zs 1,  xx部 --停止继续向下匹配连接

 


优点:

半连接 semi-join :只取出表中匹配的数据,从而减少内存的占用

 

操作

如左半连接 left-semi-join

select * from emp e left semi join dept d on e.dept_id=d.id;

相当于

select * from emp where id in (select id from dept);

相关文章:

  • 2021-11-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-02
  • 2022-02-10
相关资源
相似解决方案