1. 获取表数据
- 表A数据
- 表B数据
2. 根据表A,关联表B,根据表B的CreateTime获取最新的一条数据
SELECT StudentInfo.*, StudentScore.*
FROM StudentInfo
OUTER APPLY (SELECT TOP(1) * FROM Score WHERE StudentId = StudentInfo.StudentId ORDER BY CreateTime) StudentScore
同理,根据CreateTime获取两条数据
SELECT StudentInfo.*, StudentScore.*
FROM StudentInfo
OUTER APPLY (SELECT TOP(2) * FROM Score WHERE StudentId = StudentInfo.StudentId ORDER BY CreateTime) StudentScore