【问题标题】:Retrieve rows from a table which has multiple dependencies从具有多个依赖项的表中检索行
【发布时间】:2020-04-11 04:12:52
【问题描述】:

我正在使用 Oracle 11g DB,我需要从表 A 中检索行 id 2,因为它在表 B 上有两行,其中表 D 的状态为真 TRUE。 无法检索表 A 中的第 1 行,因为它有两行,但在这两行中,表 D 的行的状态为 FALSE。

如何从单个 sql 选择查询中选择上述行?

表 A

Id  | FName      | LName
-----------------------------
1   | Jhone      | A
2   | Alice      | B
3   | Bob        | C 

表 B

A.Id  | order_id    
--------------
1     | 1     
1     | 2     
2     | 3
2     | 1      

表 C

B.order_id  | order_type    
--------------
1           | X     
2           | Y     
3           | Z 

表 D

C.order_type  | order_status    
--------------
X           | TRUE     
Y           | FALSE     
Z           | TRUE

【问题讨论】:

    标签: sql oracle select oracle11g


    【解决方案1】:

    这将返回所有a,其中没有错误d,这就是我认为你想要的:

    select a.*
    from a
    where not exists (select 1
                      from b join
                           c
                           on b.order_id = c.order_id join
                           d
                           on c.order_type = d.order_type
                      where b.id = a.id and
                            d.order_status = 'false'
                     );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-22
      • 2012-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多