通过此查询,我遇到了错误
ERROR: missing FROM-clause entry for table "inv"
LINE 1: update stock_picking set invoice_status = inv.date_invoice
我的目标是在满足所有条件的情况下将stock_picking post_date设置为inv.date_invoice,是否可以使用SQL?
update stock_picking set posting_date = inv.date_invoice
where id in
(
SELECT DISTINCT sp.id
FROM stock_picking sp
INNER JOIN stock_move sm ON sp.id = sm.picking_id
INNER JOIN sale_order_line sol ON sm.sale_line_id = sol.id
INNER JOIN sale_order so ON sol.order_id = so.id
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
sp.posting_date != inv.date_invoice
and sm.posting_date != inv.date_invoice
and sp.state = 'done'
and inv.state in ('open', 'paid')
)
答案 0 :(得分:1)
您可以在下面尝试
update stock_picking S set posting_date = A.date_invoice
from
(
SELECT DISTINCT sp.id, inv.date_invoice
FROM stock_picking sp
INNER JOIN stock_move sm ON sp.id = sm.picking_id
INNER JOIN sale_order_line sol ON sm.sale_line_id = sol.id
INNER JOIN sale_order so ON sol.order_id = so.id
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
sp.posting_date != inv.date_invoice
and sm.posting_date != inv.date_invoice
and sp.state = 'done'
and inv.state in ('open', 'paid')
) A where S.id=A.id
答案 1 :(得分:1)
您需要一个UPDATE ... FROM
才能从更新中的查询内部引用该列。稍后在WHERE
子句中将它们连接。
UPDATE stock_picking u SET posting_date = t.date_invoice
FROM (
SELECT DISTINCT ON (sp.id)
sp.id, inv.date_invoice
FROM stock_picking sp
INNER JOIN stock_move sm ON sp.id = sm.picking_id
INNER JOIN sale_order_line sol ON sm.sale_line_id = sol.id
INNER JOIN sale_order so ON sol.order_id = so.id
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
sp.posting_date != inv.date_invoice
and sm.posting_date != inv.date_invoice
and sp.state = 'done'
and inv.state in ('open', 'paid')
ORDER BY sp.id, inv.date_invoice DESC
) t
WHERE u.id = t.id
由于我不知道您的模型,所以我假设stock_picking
和account_invoice
之间的关系可能是1:N,因此使用DISTINCT ON
将仅返回一个{{1} } date_invoice
。您可以使用id
+ GROUP BY
聚合函数来获得相同的结果。
请注意MAX/MIN
和t
表别名的使用。
答案 2 :(得分:1)
由于更新查询正试图从u
表中获取根本没有连接的数据,所以您会收到错误消息。
您也可以对更新查询进行联接。
inv