SQL 在何处过滤嵌套查询

发布于 2024-09-17 11:08:23 字数 348 浏览 4 评论 0原文

我有一个如下所示的查询:

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 得到相同的结果。 我不需要在巨大的内部查询为空的地方插入行,因为 tblX 不能在那里接受空值。我觉得我应该能够过滤该列,但我无法完全正确地理解语法。

I have a query that looks like this:

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

The problem is that last line.
It tells me that c is an invalid column name. using y.c as well, to the same result.
I need to not inset rows where that giant inner query is null, because tblX cannot accept nulls there. I feel like I should be able to filter on that column, but I can't quite get the syntax right.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦爱 2024-09-24 11:08:23

您可能需要双重嵌套此查询。

另外,取决于您的 DBMS,但您应该检查 C IS NOT NULL

您正在使用两个没有连接的表。如果您告诉我们您想要实现什么目标,我们可以提供更好的帮助。

You will probably need to double-nest this query.

Also, depends on your DBMS, but you should be checking C IS NOT NULL

You are using two tables without a join. If you tell us what you are trying to achieve, we can help better.

蓝眸 2024-09-24 11:08:23

最终起作用的是双重嵌套查询。

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

What ended up working is double nesting the query.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文