sql - 哪个sql查询是正确的
问题描述
我有一张带员工的表 A,其中 id-primary key,boss_id - foreign id。我想撤回没有雇员的雇主。哪个查询是正确的?
select a1.*
from A a1
inner join A a2 on a1.id = a2.boss_id
where a2.id is null
select a1.*
from A a1
left join A a2 on a1.id = a2.boss_id
where a2.id is null
select a1.*
from A a1
inner join A a2 on a1.boss_id = a2.id
where a2.id is null
select a1.*
from A a1
left join A a2 on a1.boss_id = a2.id
where a2.id is null
或者每一个都不正确
解决方案
为什么不直接not exists
?所有记录都来自A a1
哪里a1
是无人老板:
select a1.*
from A a1 where not exists (select 1
from A a2
where a2.boss_id = a1.id)
推荐阅读
- c# - 在 UDP 数据报中接收 0x1A
- visual-studio-2019 - 如何在 VS 2019 中保存启动项目配置?
- javascript - 通过 URL 传递带有 id 的选定选项值
- elasticsearch - 在复合聚合中使用嵌套字段
- inno-setup - AppVersion 未显示在 InnoSetup 窗口标题栏上
- python - Python - 从列表中替换 IP 地址中的最后一个八位字节
- python-3.x - 基于 Altair 中的选择突出显示地图区域
- vim - 如何在 VsCode 中修改/更改 vimrc 文件?
- unit-testing - 单元/功能测试是否应该假设未测试的应用程序部分运行正常?
- angular - Meteor-Angular:Meteor.User 不可分配给用户类型