我下面有一个查询,它似乎一次隐式连接 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')
但是我还没有那个值,因为还没有加入 t,对我来说这就像先有蛋再有鸡的问题..
这是查询的更完整版本:
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
此查询以其当前形式工作,它是我没有编写的遗留代码。 代码已被清理。
最佳答案
您可以将此类条件移至 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
关于sql - 将隐式连接转换为显式连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55336023/