0

我有一个问题,我有 4 个需要加入的表,我已经为其中 3 个进行了加入(我们称它们为 table1、table2、table3),加入直到此时工作正常,问题出在最后一张表,在这里我需要询问表号 4 上是否存在记录(这是外键之间的组合,用于连接其他 3 个表加上一个额外的列值,未记录所有记录),然后如果记录存在于表 4 中,我需要显示其内容“表 4 中的一个特定列”,否则只需将其显示为空白。以前有人做过吗?

这是前 3 个表连接的查询。

SELECT
TABLE1.NUMPROD as BATCH_ID,
TABLE2.EQPCODE as Tank,
TABLE1.CODEPROD as Product_Code,
TABLE1.XFIELD_03 as Sequence,
TABLE3.ITEM_CONSUMED as ITEM,
TABLE3.CONSUMPTION_QUANTITY as QUANTITY

FROM 
TABLE1 
INNER JOIN TABLE3 on TABLE1.NUMPROD = TABLE3.ORDERID
INNER JOIN TABLE2 ON TABLE3.ORDERID = TABLE2.NUMBERLOT

AND TABLE1.ETAT = 'F' 
AND TABLE1.DATECREATION BETWEEN ('20191001') AND ('20191004') 
AND TABLE1.XFIELD_03 IS NOT NULL
AND TABLE3.CONSUMPTION_QUANTITY > 0 
and TABLE2.VALUE = 'ASSIGNED'
4

1 回答 1

0

像这样的东西怎么样:

SELECT
TABLE1.NUMPROD as BATCH_ID,
...
TABLE3.CONSUMPTION_QUANTITY as QUANTITY,
--                                           --> from this line ...
(select some_column 
 from table4 t4
 where t4.some_id = table1.some_other_Id
) table_4_column                      
--                                           --> ... to this line
FROM 
TABLE1 JOIN ...

从中获取数据的查询最多table4 只能返回一个值;否则,你会得到too-many-rows错误。

于 2019-10-28T18:15:40.540 回答