我想知道如何获得显示所有订单,与每个订单关联的所有发票以及与每个发票关联的所有付款的结果。我收到了订单及其相关发票,但只收到第一笔付款,而不是与一个发票相关的每笔付款。有什么建议吗?

SELECT orders.n_o, invoices.n_in, payments.n_pay
FROM orders
LEFT JOIN invoices ON order.n_o = invoices.n_o_fk
LEFT JOIN payments ON invoices.n_in = payments.n_pay_fk
GROUP BY invoices.n_in;

最佳答案

大概,您不需要GROUP BY

SELECT o.n_o, i.n_in, p.n_pay
FROM orders o LEFT JOIN
     invoices i
     ON o.n_o = i.n_o_fk LEFT JOIN
     payments p
     ON i.n_in = p.n_pay_fk ;

关于mysql - SQL从第三张表中获取关联数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58916110/

10-11 05:21