【问题标题】:Oracle joins used for with or without用于有或无的 Oracle 连接
【发布时间】:2018-07-23 01:25:09
【问题描述】:

我正在尝试在 Oracle 查询中使用连接,但我不确定如何返回带有或不带有该连接条件的所有记录。以下是查询

Select req.barcode,contact.barcode,inst.barcode from entity_view req
join entity_association_view assreq ON assreq.entity_id_1 = req.entity_id 
join entity_view contact on assreq.entity_id_2 = contact.entity_id and contact.entity_type_name = 'CONTACT'
join entity_association_view assins ON assins.entity_id_2 = contact.entity_id 
join entity_view inst on assins.entity_id_1 = inst.entity_id and inst.entity_type_name = 'INSTITUTION'
where req.entity_type_name = 'REQUEST' 

这里它返回只有联系人的请求。但我也想要没有联系人的请求。我不熟悉子查询,我应该在这里使用它来获取所有有或没有联系的记录

我也使用了左连接,但它带来了像

这样的记录
Select distinct req.barcode,contact.barcode,inst.barcode from entity_view req
left join entity_association_view assreq ON assreq.entity_id_1 = req.entity_id 
left join entity_view contact on assreq.entity_id_2 = contact.entity_id and contact.entity_type_name = 'CONTACT'
left join entity_association_view assins ON assins.entity_id_2 = contact.entity_id 
left join entity_view inst on assins.entity_id_1 = inst.entity_id and inst.entity_type_name = 'INSTITUTION'
where req.entity_type_name = 'REQUEST' and req.barcode = 'GET0127' order by req.barcode

【问题讨论】:

  • 试试LEFT JOIN
  • 将所有joins 更改为left joins。
  • 看起来像一个没有联系我的请求
  • @GordonLinoff 我已经尝试过左连接,但它带来了我在屏幕截图中放入的记录。它应该只有 GET0127 CON001 INS002
  • @Paparazzi 我无法让你感到抱歉

标签: sql oracle select


【解决方案1】:

这取决于您希望如何分析您的数据。

可以在没有任何子查询的情况下完成诸如您正在做的事情之类的查询。当我是一名软件工程师实习生时,我看到了一些与您的查询语句类似但没有任何子查询的查询。

子查询允许划分数据分析或数据细化。

如果您想了解更多关于子查询的信息,请访问 link ,这是 StackOverFlow 上关于对子查询的理解的帖子。使用 SQL W3schools 链接并阅读有关 IN、LIKE、HAVING、ANY 和 ALL SQL 命令的信息以启动子查询。

使用子查询还是不使用子查询,由您决定。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    • 2010-09-17
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多