SQL从第三张表中获取关联数据

时间:2019-11-18 13:50:51

标签: mysql sql

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

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;

1 个答案:

答案 0 :(得分:1)

大概您不希望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 ;