我正在尝试在“PROD_CD”上加入四个单独的查询,以将正确的输出返回到一个查询中,以防止之后必须将查询合并到另一种语言中。使用当前的(我已经尝试了很多变体,都有各种问题),我收到了很多重复的结果和不同数量的重复结果。
这是我一直在尝试的当前查询(所有日期函数都用于确定一段时间内的数据集 - 数据库非常旧并使用 Clarion 时间):
$query_ats = "SELECT
plog.prod_cd as prod_id,
ord_log.ORDER_QTY as total_so,
ediordlg.ORDER_QTY as total_edi_so,
inv_data.IN_STOCK as in_stock
FROM plog
INNER JOIN ord_log
ON plog.prod_cd = ord_log.prod_cd
INNER JOIN ediordlg
ON plog.prod_cd = ediordlg.prod_cd AND ord_log.prod_cd = ediordlg.prod_cd
INNER JOIN inv_data
ON plog.prod_cd = inv_data.prod_cd AND ord_log.prod_cd = inv_data.prod_cd AND ediordlg.prod_cd = inv_data.prod_cd
WHERE
inv_data.CLASS_CD = 'ALG7'
AND
dateadd(day, plog.EST_DT, '18001228') BETWEEN getdate() and dateadd(day, $x, getdate())
AND
dateadd(day, ord_log.SHIP_DT, '18001228') BETWEEN getdate() and dateadd(day, $x, getdate())
AND
dateadd(day, ediordlg.SHIP_DT, '18001228') BETWEEN getdate() and dateadd(day, $x, getdate())
GROUP BY plog.prod_cd, plog.log_qty, ord_log.ORDER_QTY, ediordlg.ORDER_QTY, inv_data.IN_STOCK
ORDER BY plog.prod_cd ASC";
这是它输出的示例:
数组 ( [prod_id] => ALG-809
[total_so] => 4 [total_edi_so] => 46 [in_stock] => 0 ) 数组 ( [prod_id] => ALG-809
[total_so] => 6 [total_edi_so] => 46 [in_stock] => 0 ) 数组 ( [prod_id] => ALG-809
[total_so] => 7 [total_edi_so] => 46 [in_stock] => 0 )
以下是返回正确结果的四个单独的查询:
$query_stock = "SELECT
prod_cd,
inv_data.DESCRIP,
inv_data.IN_STOCK
from
inv_data
where
inv_data.CLASS_CD = 'ALG7'
ORDER BY
inv_data.prod_cd ASC";
$query_po = "SELECT
plog.prod_cd,
SUM(plog.log_qty) as total_po
FROM
plog JOIN inv_data ON plog.prod_cd = inv_data.prod_cd
WHERE
inv_data.CLASS_CD = 'ALG7'
AND
dateadd(day, EST_DT, '18001228') BETWEEN getdate() and dateadd(day, $x, getdate())
GROUP BY
plog.prod_cd
ORDER BY
plog.prod_cd ASC";
$query_so = "SELECT
ord_log.prod_cd,
SUM(ord_log.ORDER_QTY) as total_so
FROM
ord_log JOIN inv_data ON ord_log.prod_cd = inv_data.prod_cd
WHERE
inv_data.CLASS_CD = 'ALG7'
AND
dateadd(day, SHIP_DT, '18001228') BETWEEN getdate() and dateadd(day, $x, getdate())
GROUP BY
ord_log.PROD_CD
ORDER BY
ord_log.prod_cd ASC";
$query_edi = "SELECT
ediordlg.prod_cd,
SUM(ediordlg.ORDER_QTY) as total_so_EDI
FROM
ediordlg JOIN inv_data ON ediordlg.prod_cd = inv_data.prod_cd
WHERE
inv_data.CLASS_CD = 'ALG7'
AND
dateadd(day, SHIP_DT, '18001228') BETWEEN getdate() and dateadd(day, $x, getdate())
GROUP BY
ediordlg.PROD_CD
ORDER BY
ediordlg.prod_cd ASC";
我确定这是我正在使用的 JOIN,但我一生都无法弄清楚。有什么建议么?谢谢!