MySQL 多行连接
我在连接表时遇到问题,但尚未解决。不知怎的,我的印象是它比我想象的更简单。无论如何:
我有三个表:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许我把事情过于复杂化了,但是使用两个表并生成总和总是会导致错误的结果,即一个订单有 10 个订单行和 1 个付款行 =>付款金额将增加10倍。我想你必须使用如下所示的子选择(你没有使用表“orders”中的任何内容,而是使用了 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):
我认为您想要做的是获取所有项目的总和以及所有付款的总和,然后将它们链接在一起。子选择可以做到这一点。
像这样的东西:(ps我手头没有数据库,所以它可能不是有效的sql)
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)