我有下表:
fileId,studentId,
给定一个特定的studentId,我怎样才能获得表中最大的fileId? 我看到了另一个问题:
val_hex = 0xBEEF
val_str = convet(val_hex) # val_str = 'BEEF'
这将给出id最大的行。但是给定学生ID最大的身份证呢?在内部查询或外部查询中匹配学生是否更好?
答案 0 :(得分:1)
这似乎最简单:
SELECT studentId, MAX(ID)
FROM TableA
GROUP BY studentId
或使用过滤选项:
WITH CTE AS
(
SELECT studentId, MAX(ID)
FROM TableA
GROUP BY studentId
)
SELECT * FROM CTE WHERE studentId ...
答案 1 :(得分:1)
...给定学生ID的最大ID
SELECT MAX(fileId)
FROM table
WHERE studentId = 123
如果你想要过滤,那么123就是学生。
说有三行,其中studentId = 3,但是如果fileId = {4,5,6},我如何得到行{fileId,studentId} = {6,3}
SELECT MAX(fileId), studentId
FROM table
WHERE studentId = 3
GROUP BY studentId
答案 2 :(得分:1)
如果你需要整行:
select * from table where studentId = your_known_id order by fileId desc limit 1
答案 3 :(得分:1)
试试这个:
SELECT *
FROM (
SELECT
id,
ROW_NUMBER () OVER (PARTITION BY studentId ORDER BY id desc) rnk
FROM table) a
WHERE a.rnk = 1;
答案 4 :(得分:1)
我认为这段代码也适用
SELECT max(field) from table_name where studenId = <someID>