Sybase SQL 动态选择(连接?)关系数据库
我正在编写一个网页,以旧的成绩册格式显示学生的成绩,也就是说,它显示学生姓名,然后显示他们每项作业的分数(连续),然后每个学生重复一行。我的表格设置如下(缩短为必要信息):
表格:作业
*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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该会为您提供所需的数据。然后,您可以研究将其转换为所需的显示格式,这在您的应用程序中可能比在数据库中更容易。
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.