数据库中的多个联接

发布于 2024-10-30 09:32:18 字数 1140 浏览 1 评论 0原文

这种情况很难解释,但我会尽力而为。

对于学校,我们必须创建一个 Web 应用程序(用 PHP 编写),让教师可以管理学生的项目并允许学生进行同行评估。由于学生众多,每个项目都有多个项目组(当然,您应该只对自己的组成员进行同行评估)。

我的数据库结构目前如下所示:

用户:包含所有用户信息(user_id 是主要的)
表:项目:包含project_id、名称、描述和开始日期。

到目前为止,这非常容易。但现在变得更加困难了。

:包含一个group_id、一个组名,并且由于组是特定于项目的,因此它还包含一个project_id。
groupmembers:一个组包含多个用户,但用户可以位于多个组中(因为他们可以在多个项目中处于活动状态)。所以这个表包含一个 user_id 和一个 group_id 来链接它们。

最后,管理员可以决定用户何时需要进行同行评估以及他们有多少时间进行评估。因此,最后一个表evaluations包含evaluation_id、开始和结束日期以及project_id(实际评估存储在第六个表中,目前不相关)。

我认为这是一个很好的设计,但是当我实际上必须使用这些数据时,它会变得更加困难。我想显示您仍需要填写的评估列表。您唯一知道的是您的 user_id,因为它存储在会话中。

所以必须这样做:
1) 对组成员运行查询以查看用户属于哪些组。
2) 根据此结果,对组运行查询以查看这些组与哪些项目相关。
3)现在我们知道用户所在的项目,应该查询评估表以查看该项目是否正在进行评估。
4)我们现在知道有哪些评估可用,但是现在我们还需要检查第六个表以查看用户是否已经完成了此评估。

所有这些步骤都依赖于彼此的结果,因此它们都应该包含自己的错误处理。一旦用户选择了他们想要填写的评估(评估 ID 将通过 GET 发送),则必须运行大量新查询来检查该成员在其组中拥有哪些用户,并且必须进行评估并进行另一次检查查看哪些其他组成员已被评估)。

如您所见,这非常复杂。包含所有错误处理后,我的脚本将变得一团糟。有人告诉我“视图”在这种情况下可能会有所帮助,但我真的不明白为什么这会对我有帮助。

有没有好的方法可以做到这一点?

非常感谢!

This situation is pretty difficult to explain, but I'll do my best.

For school, we have to create a web application (written in PHP) which allows teachers to manage their students' projects and allow these to make peer-evaluation. As there are many students, every projects has multiple projectgroups (and ofcourse you should only peer-evaluate your own group members).

My databasestructure looks like this at the moment:

Table users: contains all user info (user_id is primary)
Table: projects: Contains a project_id, a name, a description and a start date.

So far this is pretty easy. But now it gets more difficult.

Table groups: Contains a group_id, a groupname and as a group is specific for a project, it also holds a project_id.
Table groupmembers: A group contains multiple users, but users can be in multiple groups (as they can be active in multiple projects). So this table contains a user_id and a group_id to link these.

At last, admins can decide when users need to do their peer-evaluation and how much time they have for it. So there is a last table evaluations containing an evaluation_id, a start and end date and a project_id (the actual evaluations are stored in a sixth table, which is not relevant for now).

I think this is a good design, but it gets harder when I actually have to use this data. I would like to show a list of evaluations you still have to fill in. The only thing you know is your user_id as this is stored in the session.

So this would have to be done:
1) Run a query on groupmembers to see in which groups the user is.
2) With this result, run a query on groups to see to which projects these groups are related.
3) Now that we know what projects the user is in, the evaluations table should be queried to see if there are ongoing evaluations for this projects.
4) We now know which evaluations are available, but now we also need to check the sixth table to see if the user has already completed this evaluation.

All these steps are dependent on the result of each other, so they should all contain their own error handling. Once the user has chosen the evaluation they wish to fill in (a evaluationID will be send via GET), a lot of new queries will have to be run to check which users this member has in his group and will have to evaluate and another check to see which other groupmembers are already evaluated).

As you see, this is quite complex. With all the errorhandling included, my script will be a real mess. Someone told me a "view" might help in this situation, but I don't really understand why this would help me here.

Is there a good way to do this?

Thank you very much!

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

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

发布评论

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

评论(2

情绪失控 2024-11-06 09:32:18

你想得太程序化了。

您的所有条件都应该轻松输入到 sql 语句的单个 where 子句中。

您最终将得到要评估的项目的单个列表。只有一个列表,只有一套错误处理。

you are thinking too procedurally.

all your conditions should be easily entered into one single where clause of a sql statement.

you will end up with a single list of the items to be evaluated. only one list, only one set of error handling.

痴梦一场 2024-11-06 09:32:18

不确定这是否完全正确,但尝试这个基本方法。我没有针对实际数据库运行此命令,因此可能需要调整语法。

select p.project_name
from projects p inner join evaluations e on p.project_id = e.project_id
where p.project_id in (
select project_id 
from projects p inner join groups g on p.project_id = g.project_id
inner join groupmembers gm on gm.group_id = g.group_id
where gm.user_id = $_SESSION['user_id'])

另外,您需要确保在将 user_id 作为查询的一部分时正确转义它,但这完全是另一个主题。

Not sure if this is exactly right, but try this basic approach. I didn't run this against an actual database so the syntax may need to be tweaked.

select p.project_name
from projects p inner join evaluations e on p.project_id = e.project_id
where p.project_id in (
select project_id 
from projects p inner join groups g on p.project_id = g.project_id
inner join groupmembers gm on gm.group_id = g.group_id
where gm.user_id = $_SESSION['user_id'])

Also, you'll need to make sure that you properly escape your user_id when making it a part of the query, but that is a whole other topic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文