gpt4 book ai didi

mySQL Schema 设计建议

转载 作者:可可西里 更新时间:2023-11-01 08:49:33 27 4
gpt4 key购买 nike

问题:如果文档中包含角色,我如何使用下面的架构获取尚未签署文档的用户列表?

背景:

我有一个用户表。用户有角色。这是一个简单的设置:

Role table:           Role_ID | Name
User table: User_ID | Name
Users_Roles table: User_ID | Role_ID

现在我需要添加一个文档表:

Documents table:  Document_ID | Name

我需要知道用户何时在文档上签字:

Documents_Users table: User_ID | Document_ID

到目前为止它非常简单。

但现在变得困难了——我需要对文档添加限制,这样我就可以将文档分配给多个角色:

即Document_ID 1 用于 Role_ID (3,4,5)。

我猜我需要做

Documents_Roles table: Role_ID | Document_ID

但是 - 这是我不知道的难点:如果文档中包含角色,我如何使用上面的模式获取尚未签署文档的用户列表?

最佳答案

Your structure is correct :

SELECT Users_Roles.User_ID, Documents_Roles.Document_ID
FROM Documents_Roles -- get some documents
-- JOIN Documents ON (Documents.Document_ID = Documents_Roles.Document_ID) -- if you need details from the Documents table
JOIN Users_Roles USING (Role_ID) -- get all users expected to sign them
-- JOIN Users ON (Users.User_ID = Users_Roles.User_ID) -- if you need details from the Users table
LEFT JOIN Documents_Users ON
Documents_Users.Document_ID = Documents_Roles.Document_ID AND
Documents_Users.User_ID = Users_Roles.User_ID -- find whether they have signed it
WHERE
Documents_Users.User_ID IS NULL -- only those expected users who have *not* signed it
-- AND Documents_Roles.Document_ID = some_id
;

关于mySQL Schema 设计建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17596378/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com