多部分标识符无法与子查询绑定在内部联接上

时间:2013-12-18 21:00:26

标签: sql sql-server subquery inner-join

我正在尝试加入主表只有相关表的第一条记录,所以我只得到一个结果

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

我已经删除了查询的所有其他部分,因为这是导致错误的部分。

1 个答案:

答案 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