mysql - 在行级别比较标记字段

标签 mysql

我有来自 TableA 的以下一组数据

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);

DEMO

关于mysql - 在行级别比较标记字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46350298/

相关文章:

php - 用逗号分隔具有相同行数的值

javascript - 使用 Node.js 在 html 表中显示 mysql

php - SQLSTATE[42S22] : Column not found: 1054 Unknown column one to many(inverse) relation laravel

mysql - 可以有 n 列吗?

php - 使用 PHP 在 mysql 表中搜索标签

php - 仅限管理员访问网页

mysql - 多行返回相同的 ID,只需要一个

php - 部分匹配 MySQL 查询

C# 和 MySQL - 字符串替换

mysql - MySQL 中是否有与 Oracle 中的 WITH 关键字等效的关键字