简体   繁体   English

数据库设计-存储要求

[英]Database Design - Storing Requirements

Suppose we want to store university courses and their entry requirements in a database. 假设我们要将大学课程及其入学要求存储在数据库中。 So for example BSc Mathematics, BSc Fine Art, MSc Computer Science etc. 例如,理学士数学,理学士艺术,计算机科学理学硕士等。

Each course has it's own set of requirements, and might have a different number of non-shared requirements. 每个课程都有自己的一套要求,并且可能有不同数量的非共享要求。 For example, to be eligible for the BSc Maths you might need a A in Maths and a B in Physics. 例如,要获得BSc数学的资格,您可能需要数学A和物理B。 Whereas the BSc Fine Art might require a A in Art and that the user has a portfolio. BSc美术可能需要A A艺术奖,并且用户拥有作品集。 The MSc might have a minimum age of 25 etc. MSc的最小年龄可能为25等。

Suppose we then have a student who has his own set of attributes. 假设我们有一个学生,他有自己的一套属性。 So they might have a A in Maths and a B in Physics and a C in Chemistry and be of age 19. 因此,他们可能在19岁时就获得了数学A,物理学B和化学C的佳绩。

How can we structure our database such that it is geared towards efficient lookups. 我们如何构造我们的数据库,使其能够进行有效的查找。 And given a student's attributes how can we retrieve all the courses which they are eligible for? 给定学生的属性,我们如何检索他们有资格参加的所有课程?

On an abstract level we are looking for all items whose requirements are a subset of the attributes given. 从抽象的角度来看,我们正在寻找所有需求都是给定属性子集的项目。

I'd like to implement this in MySQL. 我想在MySQL中实现这一点。 The schema could be: 该模式可以是:

courses 课程

id ID

name 名称

requirements 要求

course_id course_id

subject 学科

grade 年级

But then how to do query the table to get all eligible courses when the student has a A in Subject 1, a B in Subject 2 etc. 但是,当学生在科目1中有A,科目2中有B等时,如何查询表以获取所有合格的课程。

For a course to be a match, each one of its requirements must be satisfied. 为了使一门课程符合要求,必须满足其每个要求。

Thanks in advance, I hope my explanation isn't too confusing. 在此先感谢您,我希望我的解释不要太混乱。

Okay. 好的。 I think you need a subject list, with a bunch of subject id's. 我认为您需要一个主题列表,其中包含许多主题ID。

Now, for the query, you will start with (or generate) a bunch of subject id's and grades. 现在,对于查询,您将从一堆主题ID和等级开始(或生成)。 The easiest thing to do is to make this into a temporary table with those columns. 最简单的方法是使用这些列将其放入一个临时表中。

Now, you can do your query, joining the subject id's, and adding a "where temp.grade <= course.grade. 现在,您可以查询,加入主题ID并添加“ where temp.grade <= course.grade。

The trick to this is to count the rows. 诀窍是计算行数。 If it has the same number of rows as the course has specified, then you have a successful match. 如果其行数与课程指定的行数相同,则说明匹配成功。

Is that enough to get you going? 这足以使您前进吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM