sql - 将隐式连接转换为显式连接

标签 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')

但是我还没有那个值,因为还没有加入 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/

相关文章:

java - (Spark skewed join) 如何在没有内存问题的情况下连接两个具有高度重复键的大型 Spark RDD?

<label> 里面的 Django &lt;input&gt;

sql - SQL 中 > 0 是什么意思?

Mysql查询。更新、乘法、连接和选择(?)全部放在一起

sql - SQLite:使用分组依据更新列

mysql - 连接行以避免 Mysql Select 查询中的冗余

plsql - 在 PL/SQL 中,隐式游标是否会无法关闭?

c# - 在方法返回上使用隐式类型语法

php - 使用 SQL windows 函数编写查询来计算一个国家/地区城市人口的运行总数

sql - 有没有办法在 SQL 中找到活跃用户?