簡體   English   中英

SQL對嵌套查詢進行過濾的位置

[英]SQL Where to filter on Nested query

我有一個看起來像這樣的查詢:

Insert Into tblX (a,b,c)
Select x as a, y as b
   (select top 1 whatever from tblZ as z where z.aID  = y.aID order by z.s desc) as c
from tblY as y
where y.foo = 'bar'
AND c <> NULL

問題是最后一行。 它告訴我c是無效的列名。 同樣使用yc可以得到相同的結果。 我不需要在大型內部查詢為null的地方插入行,因為tblX不能在那里接受null。 我覺得我應該可以在該列上進行過濾,但是我不太了解語法。

您可能需要對此查詢加倍嵌套。

另外,取決於您的DBMS,但是您應該檢查C IS NOT NULL

您正在使用兩個沒有聯接的表。 如果您告訴我們您要達到的目標,我們將為您提供更好的幫助。

最終工作是雙重嵌套查詢。

Insert Into tblX (a,b,c)
  Select a,b,c from
    (select x as a,
           y as b,
     (select top 1 whatever from tblZ as z where z.aID  = y.aID order by z.s desc) as c
      from tblY as y where y.foo = 'bar') as foobar
  Where c IS NOT NULL

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM