我必须创建一个mysql数据库来存储如下值:
id bigint(18) auto_increment,
NameOfStudent char(40),
UnixTime_YearOfExam bigint(18),
percentageScored decimal(5,2),
marksSubJect1 decimal(5,2),
marksSubJect2 decimal(5,2),
marksSubJect3 decimal(5,2),
查询将是:
Select *
from table
where NameOfStudent='xyz'
order by UnixTime_YearOfExam desc, percentageScored desc limit 3
每个学生大约有2000名学生有不同年份的记录。因此每个学生大约有5-10条记录。 我的问题是:
此表上的索引应该是什么,因此上述查询很快。 where子句和排序
如何选择每位学生的最后3条记录(按YearOfExam desc限制3排序),而无需为每位学生运行个别查询? 这样一个查询的结果将如下所示:
student_1, yearOfExam2015, percentageScored
student_1, yearOfExam2014, percentageScored
student_1, yearOfExam2013, percentageScored
student_2, yearOfExam2015, percentageScored
student_2, yearOfExam2014, percentageScored
student_2, yearOfExam2013, percentageScored
and so on
答案 0 :(得分:0)
我会使用“marks_id”字段索引“marks”表,该字段会自动递增。只要两个表中都有“student_name”或“student_id”字段,就可以执行INNER JOIN以在一个查询中获取所需的所有信息。