将隐式联接转换为显式

时间:2019-03-25 10:45:19

标签: sql join implicit

下面我有一个查询,该查询似乎一次隐式地联接了3个表,我试图将其重写为使用显式联接,但我无法理解,这似乎是针对一个联接依赖于尚未完成的联接。

这是仅隐式联接可以容纳的方案吗?

我遇到的问题是这种和平:

AND((t.object_type = 'SOME_VALUE' and i.pro_prod_id=400 and i.gbcert_id || '-Packing' = ta.gbcert_id)
OR (t.object_type in ('V1','V2','V3') and i.gbcert_unique_id = ta.gbcert_id)) 

您可以看到我们正在根据t.object_type = 'SOME_VALUE't.object_type in ('V1','V2','V3')

决定要加入的列

但是我还没有那样的价值,因为还没有Join on,对我来说,这似乎是鸡蛋之前的鸡蛋。

这是查询的更完整版本:

FROM TRANSACTION t, USG_Award ta,
inbox i,
stores s,
clients cl,
client_types ct
WHERE ct.client_type = cl.client_type
AND ct.usg_aggregation_client IS NULL
AND i.orig_store_code = s.sto_store_code
AND i.orig_store_code   = cl.store_code
and ((t.object_type = 'SOME_VALUE' and i.pro_prod_id=400 and i.gbcert_id || '-Pack' = ta.gbcert_id)
OR (t.object_type in ('V1','V1','V1') and i.gbcert_unique_id = ta.gbcert_id))
AND ta.fk_usg_tx = t.pk_usg_tx

此查询以当前形式运行,这是我未编写的旧代码。 该代码已被清理。

1 个答案:

答案 0 :(得分:2)

您可以将这样的条件移到ON子句中:

FROM TRANSACTION t JOIN
     USG_Award ta
     ON ta.fk_usg_tx = t.pk_usg_tx JOIN
     inbox i 
     ON ((t.object_type = 'SOME_VALUE' and i.pro_prod_id = 400 and i.gbcert_id || '-Pack' = ta.gbcert_id) OR
         (t.object_type in ('V1', 'V1', 'V1') and i.gbcert_unique_id = ta.gbcert_id)
        )JOIN
     stores s
     ON i.orig_store_code = s.sto_store_code JOIN
     clients cl 
     ON i.orig_store_code = cl.store_codeJOIN
     client_types ct
     ON ct.client_type = cl.client_type
WHERE ct.usg_aggregation_client IS NULL