内联发票

时间:2018-11-23 14:51:45

标签: sql join

我进行了此SQL查询并获得了所需的销售订单,但是我仍然需要将此销售订单与其发票合并,并仅过滤那些带有state != 'draft'发票的销售订单。

但是找不到找到带有发票的销售订单的方法。

我认为有一个名为sale_order_invoice_rel的表,但无法弄清楚如何正确连接和过滤

SELECT DISTINCT so.id

FROM stock_picking sp

INNER JOIN stock_move sm

ON sp.id = sm.picking_id

INNER JOIN procurement_order po

ON sm.procurement_id = po.id

INNER JOIN sale_order_line sol

ON po.sale_line_id = sol.id

INNER JOIN sale_order so

ON sol.order_id = so.id

WHERE so.invoice_status = 'to invoice' and sp.state = 'done'
ORDER BY so.id ASC

1 个答案:

答案 0 :(得分:1)

您可以像这样加入他们。

    FROM stock_picking sp
                       INNER JOIN stock_move sm ON sp.id = sm.picking_id
                       INNER JOIN procurement_order po ON sm.procurement_id = po.id
                       INNER JOIN sale_order_line sol ON po.sale_line_id = sol.id 
                       INNER JOIN sale_order so ON sol.order_id = so.id
                       -- keep join them like this
                       INNER JOIN sale_order_invoice_rel so_inv_rel on so.id = so_inv_rel.order_id
                       INNER JOIN account_invoice inv on so_inv_rel.invoice_id = inv.id

    WHERE 

            so.invoice_status = 'to invoice'
            and sp.state = 'done'       
            and inv.state != 'draft'        

使用INNER JOIN将只显示有发票的销售订单。