Sybase SQL 动态选择(连接?)关系数据库

发布于 2024-10-30 04:36:20 字数 616 浏览 1 评论 0原文

我正在编写一个网页,以旧的成绩册格式显示学生的成绩,也就是说,它显示学生姓名,然后显示他们每项作业的分数(连续),然后每个学生重复一行。我的表格设置如下(缩短为必要信息):

表格:作业
*Assignment_PK(主键)
* 作业名称

表:学生
* Student_PK(主键)
* Student_Name

表:StudentAssignment
* SA_PK(主键
* Student_FK(学生.Student_PK)
* 分配_FK(分配.分配_PK)
* 分数

我正在尝试编写一个 SELECT 语句,该语句将打印每个作业的学生姓名和分数。我遇到的问题是,如果我 SELECT Score 作为一列,我只能获得一项作业的分数,因为在我的 WHEREAssignment_FK =Assignment_PK 中只允许我为一列选择一个分数。

我对关系数据库相当陌生,我真的可以使用一些帮助来找到解决这个问题的最佳方法。一个建议是我编写一条 SELECT 语句将所有学生选入表中,然后在表中执行 foreach 行并让它选择分数并将其放置在适当的列中。这似乎是一个缓慢且不必要的过程。有没有更简单的方法使用 JOINS?或者写一个更好的SELECT?

I am writing a web page that displays students grades in an old grade book format which is to say, it displays the Student name, then their score for each assignment (in a row) then repeats one row per student. My tables are set up as follows (shortened to necessary info):

Table: Assignment
* Assignment_PK (Primary Key)
* Assignment_Name

Table: Student

* Student_PK (Primary Key)
* Student_Name

Table: StudentAssignment
* SA_PK (Primary Key
* Student_FK (Student.Student_PK)
* Assignment_FK (Assignment.Assignment_PK)
* Score

I am trying to write a SELECT statement that will print the students name and score for each assignment. The problem I'm running into is that if I SELECT Score as a column, I only get the score for one assignment because in my WHERE Assignment_FK = Assignment_PK only allows me to pick one Score for a column.

I'm fairly new to Relational Databases and I could really use some help on the best way to tackle this. One suggestion was that I write a SELECT statement to pick all Students into a table, then do a foreach row in the table and have it select the scores and place them in the appropriate column. This seems like a slow and unnecessary process. Is there any easier way using JOINS? Or writing a better SELECT?

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

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

发布评论

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

评论(1

叶落知秋 2024-11-06 04:36:20

这应该会为您提供所需的数据。然后,您可以研究将其转换为所需的显示格式,这在您的应用程序中可能比在数据库中更容易。

SELECT s.Student_Name, a.Assignment_Name, sa.Score
    FROM Student s
        INNER JOIN StudentAssignment sa
            ON s.Student_PK = sa.Student_FK
        INNER JOIN Assignment a
            ON sa.Assignment_FK = a.Assignment_PK
    ORDER BY s.Student_Name, a.Assignment_Name

This should get you the data you need. You can then investigate pivoting it into the desired display format, which may be easier in your application than in the database.

SELECT s.Student_Name, a.Assignment_Name, sa.Score
    FROM Student s
        INNER JOIN StudentAssignment sa
            ON s.Student_PK = sa.Student_FK
        INNER JOIN Assignment a
            ON sa.Assignment_FK = a.Assignment_PK
    ORDER BY s.Student_Name, a.Assignment_Name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文