我有一个简单的表格来存储主题的结果,所以一个用户可能做了很少的主题,他们可能有不同的分数。我想创建一个存储过程来检索给定学生的最小和最大主题详细信息:
DELIMITER $$
CREATE DEFINER=`root`@`localhost`
PROCEDURE `Search_Min_Marks_For_Student`(IN Student_code SMALLINT)
BEGIN
SELECT Subject_Subject_code AS `Minimum Scored Subject`,
Subject_title AS `Subject Title`,
Min(Total_mk) AS Marks
FROM result,subject
WHERE result.Student_Student_code = Student_code AND
Subject_Subject_code=Subject_code;
END
我为最低分数写了一个,它工作正常,但是有没有办法可以将最高结果添加到同一个查询中?
-- Table structure for table `result`
--
DROP TABLE IF EXISTS `result`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `result` (
`Result_code` smallint(6) NOT NULL AUTO_INCREMENT,
`Student_Student_code` smallint(6) NOT NULL,
`Subject_Subject_code` smallint(6) NOT NULL,
`Practical_mk` smallint(6) NOT NULL,
`Assignment_mk` smallint(6) NOT NULL,
`Exam_mk` smallint(6) NOT NULL,
`Total_mk` smallint(6) NOT NULL,
`Grade` varchar(20) NOT NULL,
PRIMARY KEY (`Result_code`),
KEY `fk_Result_Subject1_idx` (`Subject_Subject_code`),
KEY `fk_Result_Student1_idx` (`Student_Student_code`),
CONSTRAINT `fk_Result_Student1` FOREIGN KEY (`Student_Student_code`) REFERENCES `student` (`Student_code`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_Result_Subject1` FOREIGN KEY (`Subject_Subject_code`) REFERENCES `subject` (`Subject_code`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `subject`
--
DROP TABLE IF EXISTS `subject`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `subject` (
`Subject_code` smallint(6) NOT NULL AUTO_INCREMENT,
`Subject_title` varchar(50) NOT NULL,
`Num_of_credits` smallint(6) NOT NULL,
`Description` varchar(100) DEFAULT NULL,
`Course_Course_code` smallint(6) NOT NULL,
`Department_Dep_code` smallint(6) NOT NULL,
PRIMARY KEY (`Subject_code`),
KEY `fk_Subject_Course1_idx` (`Course_Course_code`),
KEY `fk_Subject_Department1_idx` (`Department_Dep_code`),
CONSTRAINT `fk_Subject_Course1` FOREIGN KEY (`Course_Course_code`) REFERENCES `course` (`Course_code`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_Subject_Department1` FOREIGN KEY (`Department_Dep_code`) REFERENCES `department` (`Dep_code`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=8004 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;