sql - ORACLE - 使用左连接和内连接来编辑查询
问题描述
我正在研究这个查询并且我正在尝试,但是无论用户的扇区和船舶的扇区如何,我都无法返回所有船舶。(第 5 行)
该查询返回所有不再与用户关联的船舶,只要船舶所在的扇区与用户所在的扇区相同。
我希望所有与用户无关的船舶返回,无论用户和船舶部门如何
我试图删除innerJoin,以避免比较,但它给出了一个错误
有 3 个表:Users、Ships 和 Ships_Users
select
sh.Id,
sh.Name
from USERS user
inner join Ship sh on ltrim(rtrim(sh.Sector)) = ltrim(rtrim(user.Setor))
LEFT JOIN SHIP_USER su on su.ship_id = sh.id and su.user_id = user.id
where user.id =:userId
and su.id is null and sh.Active = 1;
解决方案
我希望所有与用户无关的船舶返回,无论用户和船舶部门如何
你似乎在问这个:
select s.*
from ship s
where not exists (select 1
from ship_users su
where su.ship_id = s.id and su.user_id = :user_id
) and
s.isactive = 1;
推荐阅读
- python - Pandas 10位数字提取
- python - 如何在不输入 python 的情况下运行 python 脚本
- object - 在 Kotlin 中,如何创建具有另一个类/接口所具有的所有功能的单个对象?
- python - 在机器学习中进行反向消除的有效方法?
- reactjs - 这是更新状态的正确方法吗?
- angular - Google Cloud Build - firebase 部署错误:“公共目录 'dist/browser' 不存在,无法将托管部署到站点 PROJECT-ID”
- flutter - 如何使用 CupertinoSliverNavigationBar Flutter 制作顶部标签栏
- scala - 解析scala宏中的类型投影
- spring-boot - java 8中如何实现异步重试机制?
- python - 保存表单时如何将数据传递给模型字段?django表单模型保存