我正在尝试加入主表只有相关表的第一条记录,所以我只得到一个结果
SELECT
*
FROM
admissions as a
inner join (select
top 1 *
from
admissionroom
where
admissionroom.admissionid = a.id
order by
admissionroom.startdate asc) as r on
a.id = r.admissionid
我已经删除了查询的所有其他部分,因为这是导致错误的部分。
答案 0 :(得分:4)
您无法在JOIN
上执行此操作,您需要使用CROSS APPLY
:
SELECT *
FROM admissions as a
CROSS APPLY (SELECT TOP 1 *
FROM admissionroom
WHERE admissionid = a.id
ORDER BY startdate ASC) as r