我有一个问题,我有4个需要连接的表,我已经为其中3个进行了连接(让它们称为table1,table2,table3),直到此点正常工作为止,连接都伴随着问题。最后一张表,在这里我需要询问表4上是否存在一条记录(这是外键'的组合,用于连接其他3个表的键与一个附加的列值,但并非所有记录都记录下来),然后如果记录存在于表4中,我需要显示其内容“来自table4的一个特定列”,否则仅将其显示为空白。有人做过吗?
这是具有前三个表联接的查询。
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'
类似这样的事情:
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
错误。