在另一个表 - MYSQL中查找分数的等级和备注

时间:2015-10-23 12:32:56

标签: mysql

我有一个名为examrecords的MYSQL表,其中包含以下内容;

Studentid Score 1 60 2 70 3 80

我有另一个名为grader的表,其中包含以下内容;

Startpoint Endpoint Grade Remarks 80 100 A Excellent 70 79 B Very Good 60 69 C Good

我想写一个查询,可以在考试记录中挑选个人分数,并将它们与评分者进行比较并分配相应的分数。任何帮助?

请使用以下脚本创建示例数据库的副本...

CREATE DATABASE Grades;

USE Grades;

CREATE TABLE examrecords
(
    StudentID    INT    NOT NULL    AUTO_INCREMENT,
    Score        INT    NOT NULL,
    PRIMARY KEY ( StudentID )
);

CREATE TABLE grader
(
    Startpoint    INT            NOT NULL,
    Endpoint      INT            NOT NULL,
    Grade         VARCHAR(1)     NOT NULL,
    Remarks       VARCHAR(50)    NOT NULL,
    PRIMARY KEY ( Grade )
);

INSERT INTO examrecords
SET Score = 60;

INSERT INTO examrecords
SET Score = 70;

INSERT INTO examrecords
SET Score = 80;

INSERT INTO grader
SET Startpoint = 80,
    Endpoint   = 100,
    Grade      = "A",
    Remarks    = "Excellent";

INSERT INTO grader
SET Startpoint = 70,
    Endpoint   = 79,
    Grade      = "B",
    Remarks    = "Very Good";

INSERT INTO grader
SET Startpoint = 60,
    Endpoint   = 69,
    Grade      = "C",
    Remarks    = "Good";

1 个答案:

答案 0 :(得分:0)

请尝试以下方法......

SELECT Studentid,
       Grade
FROM examrecords, grader
WHERE Score BETWEEN Startpoint
                AND Endpoint;

我已针对示例数据库对其进行了测试,并提供了所需的结果。

如果您有任何问题或意见,请随时添加评论。