SQL - 从最内部条件子句

时间:2017-12-22 00:25:58

标签: sql sql-server

我的SQL查询有问题,因为最内部的WHERE子句无法访问h.ID(错误:多部分标识符" h.ID"无法绑定) 。但是,我无法看到其他相关帖子与我的相似问题。

我还有其他办法吗?

SELECT * 
FROM dbo.table1 as h
LEFT JOIN
   ( SELECT * 
     FROM ( SELECT r.num * d.num AS value 
            FROM dbo.table2 r
            JOIN dbo.table3 d
            ON d.ID = r.ID
            WHERE r.ID = h.ID
          ) m
     WHERE m.value > 1000
   ) mm
ON mm.ID = h.ID;

2 个答案:

答案 0 :(得分:2)

我想你想要:

SELECT * 
FROM dbo.table1 h LEFT JOIN
     dbo.table2 r
     ON r.ID = h.ID AND r.value > 1000;

编辑:

这应该基本上是你想要的:

SELECT h.*, r.num * d.num AS value 
FROM dbo.table1 h LEFT JOIN
     dbo.table2 r 
     ON r.ID = h.ID LEFT JOIN
     dbo.table3 d
     ON d.ID = r.ID AND r.num * d.num > 1000;

答案 1 :(得分:0)

尝试:

SELECT * 
    FROM dbo.table1 as h
    LEFT JOIN
       ( SELECT * 
         FROM ( SELECT r.num * d.num AS value ,r.ID AS ID
            FROM dbo.table2 r
            JOIN dbo.table3 d
            ON d.ID = r.ID
        WHERE r.ID = d.ID
      ) m
     WHERE m.value > 1000
   ) mm
ON mm.ID = h.ID;