我有这种表并找到最大标记
STUDENT
|id | name | mark |
|1 | john | 56 |
|2 | sara | 81 |
|3 | mattew| 65 |
out假设是这样的
|id | name | mark |
|2 | sara | 81 |
但是我得到了这种输出
|id | name | mark |
|1 | john | 81 |
我在sql中写这个
SELECT id,name,MAX(mark)
FROM student;
WHERE name IN
(SELECT name
FROM student);
我该如何更正sql?
答案 0 :(得分:3)
在SQL Server中,您可以使用
SELECT TOP 1 WITH TIES *
FROM student
WHERE mark IS NOT NULL
ORDER BY mark DESC
虽然您也可以使用逻辑上等效的标准SQL
SELECT *
FROM student
WHERE mark = (SELECT MAX(mark)
FROM student)
答案 1 :(得分:0)
您可以使用此简单查询来获得相同的结果:
SELECT TOP 1 WITH TIES ID,NAME,mark
FROM STUDENT
ORDER BY MARK DESC