Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 在行级别比较标记字段_Mysql - Fatal编程技术网

Mysql 在行级别比较标记字段

Mysql 在行级别比较标记字段,mysql,Mysql,我有以下表格A中的数据集 S.No StudentID Marks Description SubjectID 1 1 50 Science 1 2 1 30 Mathematics 2 3 2 25 Science 1 4 2 25 Mathematics 2 我想进行一个查询,比较科学

我有以下表格A中的数据集

S.No  StudentID      Marks  Description  SubjectID
1             1        50   Science      1
2             1        30   Mathematics  2
3             2        25   Science      1
4             2        25   Mathematics  2
我想进行一个查询,比较科学和数学的分数,让学生只得到比数学分数更高的分数。我不应该看到那个在两门课上得分相同的学生

输出:

S.No  StudentID      Marks  Description  SubjectID
1             1        50   Science      1
2             1        30   Mathematics  2

有人能帮我做一个简单的查询吗。提前感谢。

这是可以做到的,但需要很长的时间,但我相信会有另一个很好的解决方案

select a.*
from demo a
join(
select t.StudentID
from demo t
join demo t1 on t.StudentID = t1.StudentID
and t.Description = 'Science'
and t1.Description = 'Mathematics'
and t.Marks > t1.Marks) b using(StudentID);

请参见您是否有问题中未提及的主题?如果您在和条件中都使用主题而不是描述,是否会对查询性能产生任何影响?是的,可以使用相关主题ID,对于上述查询,我建议在索引(StudentID、description、Marks)上使用复合索引