0

我有一个查询,它提取了我需要由多个 WHERE 子句过滤的大量信息。但是,我不希望我的主表(在这种情况下为 inv_data)受到所有 WHERE 子句的影响。有什么方法可以选择哪些表受哪个 WHERE 影响?

这是我当前的查询:

$query_ats = "SELECT
                i.prod_cd as product,
                i.descrip as description,
                i.in_stock as current_stock, 
                SUM(p.log_qty) as purchase_order,
                SUM(l.order_qty + e.order_qty) as total_so
            from
                inv_data as i
                inner join plog as p
                    on i.prod_cd = p.prod_cd
                inner join ord_log as l
                    on i.prod_cd = l.prod_cd
                inner join ediordlg as e
                    on i.prod_cd = e.prod_cd
            where
                i.class_cd = 'ALG7'
            AND 
            dateadd(day, p.EST_DT, '18001228') BETWEEN getdate() and dateadd(day, 14, getdate())
            AND 
            dateadd(day, l.SHIP_DT, '18001228') BETWEEN getdate() and dateadd(day, 14, getdate())
            AND 
            dateadd(day, e.SHIP_DT, '18001228') BETWEEN getdate() and dateadd(day, 14, getdate())
            group by
                i.prod_cd,
                i.descrip,
                i.in_stock,
            order by
                i.prod_cd ASC";

表 i (inv_data) 是我不希望受任何 dateadd 子句影响的主表。

谢谢!

4

1 回答 1

1

左外连接条件并将条件移动到on子句:

SELECT i.prod_cd as product,
       i.descrip as description,
       i.in_stock as current_stock, 
       SUM(p.log_qty) as purchase_order,
       SUM(l.order_qty + e.order_qty) as total_so
from inv_data i left outer join
     plog p
     on i.prod_cd = p.prod_cd and
        dateadd(day, p.EST_DT, '18001228') BETWEEN getdate() and dateadd(day, 14, getdate())
     left outer join
     ord_log l
     on i.prod_cd = l.prod_cd and
        dateadd(day, l.SHIP_DT, '18001228') BETWEEN getdate() and dateadd(day, 14, getdate()) 
     left outer join
     ediordlg e
     on i.prod_cd = e.prod_cd and
        dateadd(day, e.SHIP_DT, '18001228') BETWEEN getdate() and dateadd(day, 14, getdate())
where i.class_cd = 'ALG7'
group by i.prod_cd, i.descrip, i.in_stock,
order by i.prod_cd ASC
于 2013-01-28T20:51:52.250 回答