首页 > 解决方案 > 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;

标签: sqloracle

解决方案


我希望所有与用户无关的船舶返回,无论用户和船舶部门如何

你似乎在问这个:

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;

推荐阅读