MySQL 多行连接

发布于 2024-11-28 13:22:46 字数 702 浏览 0 评论 0原文

我在连接表时遇到问题,但尚未解决。不知怎的,我的印象是它比我想象的更简单。无论如何:

我有三个表:

orders

orderlines

payment

在“orders”中,每一行对应于客户的一个订单。每个订单都有一个 order_id,它是该表的主键。在“订单行”中,我保留订单的内容,即对订单上的产品和服务的引用。一个订单可以且通常具有多个订单行。最后,在付款中,我为支付订单的每笔交易存储一行。

理想情况下,一份订单永远不会有超过一行的对应付款。但由于客户就是客户,有人两次支付同一张发票的情况并不罕见,这表明付款表可以为一个订单包含两次或多次付款。

因此,创建一个以相关方式连接所有三个表的查询会很有用,但我还没有设法这样做。例如:

SELECT order.order_id, SUM(orderlines.amount), SUM( payments.amount) 来自订单 LEFT JOIN 订单行 ON 订单.order_id = orderlines.order_id 左连接付款 ON 订单.order_id = 付款.order_id GROUP BY Orders.order_id

此连接的目的是查明订单上产品的 SUM 是否等于付款中的 SUM。这里的问题是,两个表 payment 和 orderlines 彼此“分散”注意力,因为它们在连接时都会导致多行。

这个问题有简单的解决办法吗?

I have an issue with joining of tables that I have not managed to solve. Somehow I have the impression that it is more simple than I think. Anyhow:

I have three tables:

orders

orderlines

payments

In "orders" every line corresponds to one order made by a customer. Every order has an order_id which is the primary key for that table. In "orderlines" I keep the content of the order, that is references to the products and services on the order. One order can, and typically has, many orderlines. Finally, in payments I store one row for every transaction made to pay for an order.

One order ideally never has more than one corresponding row in payments. But since customers are customers it is not unusual that someone pays the same invoice twice, hinting that the payments table can have two or more payments for one order.

Therefore it would be useful to create a query that joins all three tables in a relevant way, but I have not managed to do so. For instance:

SELECT orders.order_id, SUM(orderlines.amount), SUM(payments.amount)
FROM orders
LEFT JOIN orderlines
ON orders.order_id = orderlines.order_id
LEFT JOIN payments
ON orders.order_id = payments.order_id
GROUP BY orders.order_id

The purpose of this join is to find out if the SUM of the products on the order equals the SUM in payments. The problem here is that the two tables payments and orderlines "distract" each other by both causing multiple rows while joining.

Is there a simple solution to this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

洋洋洒洒 2024-12-05 13:22:46

也许我把事情过于复杂化了,但是使用两个表并生成总和总是会导致错误的结果,即一个订单有 10 个订单行和 1 个付款行 =>付款金额将增加10倍。我想你必须使用如下所示的子选择(你没有使用表“orders”中的任何内容,而是使用了 id,所以我将其省略,因为所有订单都有订单行):

SELECT t1.order_id, t1.OrderAmount, t2.PaymentAmount
FROM (SELECT SUM(amount) AS OrderAmount, order_id
      FROM orderlines
      GROUP BY order_id) AS t1
LEFT JOIN (SELECT SUM(amount) AS PaymentAmount, order_id
        FROM payments
        GROUP BY order_id) AS t2 
        ON t1.order_id=t2.order_id

Maybe I'm overcomplicating things, but using both tables and producing the sum would always lead too wrong results, i.e. one order has 10 orderline rows and 1 payment rows => the payment amount is going to be added 10 times. I guess you have to use subselects like this below (you didn't use anything from your table "orders" but the id, so I left it out, because all orders have orderlines):

SELECT t1.order_id, t1.OrderAmount, t2.PaymentAmount
FROM (SELECT SUM(amount) AS OrderAmount, order_id
      FROM orderlines
      GROUP BY order_id) AS t1
LEFT JOIN (SELECT SUM(amount) AS PaymentAmount, order_id
        FROM payments
        GROUP BY order_id) AS t2 
        ON t1.order_id=t2.order_id
倾听心声的旋律 2024-12-05 13:22:46

我认为您想要做的是获取所有项目的总和以及所有付款的总和,然后将它们链接在一起。子选择可以做到这一点。

像这样的东西:(ps我手头没有数据库,所以它可能不是有效的sql)

SELECT * FROM orders
LEFT JOIN (SELECT order_id, SUM(amount) FROM orderlines GROUP BY order_id) AS ordersums
ON orders.order_id = ordersums.order_id
LEFT JOIN (SELECT order_id, SUM(amount) FROM payments GROUP BY order_id) AS paymentsums
ON orders.order_id = paymentsums.order_id;

I think what you want to do is get the sum of all the items, and the sum of all the payments, and then link them together. A sub-select is able to do this.

Something like: (ps I have no database on hand so it might not be valid sql)

SELECT * FROM orders
LEFT JOIN (SELECT order_id, SUM(amount) FROM orderlines GROUP BY order_id) AS ordersums
ON orders.order_id = ordersums.order_id
LEFT JOIN (SELECT order_id, SUM(amount) FROM payments GROUP BY order_id) AS paymentsums
ON orders.order_id = paymentsums.order_id;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文