如何在MS Access中使用多对多关系进行计算
我有一个具有以下结构的数据库。每本书可以有多个作者,每个作者可以写多本书。
[book:book_id, book_name, book_price]
[author: author_id, author_name]
[link:book_id, author_id]
{book_id 和author_id 已链接。完整的结构如下所示:https://i.sstatic.net/vIFNU.png}
现在每本书都有一个价格(货币)。价格的30%应平均分配给对本书做出贡献的每位作者。 我的问题是如何找到特定年份每个作者的总付款。
[我自己想到了一个解决方案。我只能做第 1 步。如果您可以向我提供一些提示或材料,让我可以找到如何进行此类操作,这将非常有帮助]
我的解决方案的算法是:
因此对于每个 book_id ,我需要找到中间表中具有相同book_id的author_id的数量。 (可以通过查询来完成)
如果我将 book_price 除以 number_of_author_in_book 并乘以 30/100,我会得到该书的钱,该钱将转到该书的每个作者的帐户(例如 payment_of_one_author_in_book)
对于中间表中的每个author_id,我查找相应的 book_id 并将该author_id 的 payment_of_one_author_in_book 添加到与作者 ID 对应的新变量(author_ payment_this_year),如果年份与查询年份匹配。
提前致谢
I have a database having the following structure. Each book can have multiple authors, and each author can write multiple books.
[book:book_id, book_name, book_price]
[author: author_id, author_name]
[link:book_id, author_id]
{book_id's and author_id's are linked. the complete structure is shown here: https://i.sstatic.net/vIFNU.png}
Now each book has a price (currency). 30% of the price should be equally distributed to each author who have contributed to the book.
My question is how to find the total payment for each author for a particular year.
[I thought of a solution my self. I could do only up to step 1. If you can provide me some hints or materials where I can find how do such manipulations, it would be very helpful]
Algorithm of my solution is:
So for each book_id, I need to find the number of author_id's in the middle table who has the same book_id. (could do it by query)
If I divide the book_price by number_of_author_in_book and multiply it with 30/100, I get the money for that book that will go to the each author's account for that book (say payment_of_one_author_in_book)
For each author_id in the middle table, I look up for the corresponding book_id and add the payment_of_one_author_in_book for that author_id to a new variable (author_payment_this_year) corresponding to the author ID, if the year matches to the query year.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基本上,它只是三个表的联接。唯一棘手的一点是使用 DCount 函数来计算 Link 中有多少作者共享这本书的 ID
Basically, it is just a join of the three tables. The only tricky bit is using DCount function to add up how many authors in Link share this book_ID
此示例包括别名和子查询。
This example includes aliases and subqueries.