存在的子查询

发布于 2025-01-26 07:22:47 字数 613 浏览 2 评论 0原文

我是一名从事库存管理的用户,试图返回有关产品名称,位置及其在2017年Adventure Works数据库中位置的信息。在试图运行查询时,我收到一条错误消息,该消息说我在子查询列表中的表达太多,只有在使用“存在”的子查询开始时,我才能做到这一点。我想我不明白我在做什么错,也许有人可以解释“存在”是如何工作的?有什么方法可以重写此功能,以便可以在子查询中返回两个表达式?我将在下面包含语法和错误消息。

SELECT Production.Product.Name
       ,(SELECT Production.Location.Name
         ,Production.Location.Availability
         FROM Production.Location
         WHERE Production.Location.LocationID = Production.ProductInventory.LocationID)
FROM Production.Product
    INNER JOIN Production.ProductInventory
    ON Production.Product.ProductID = Production.ProductInventory.ProductID;

I am a user working in inventory management attempting to return information regarding product name, location, and its availability at the location from the adventure works 2017 database. While trying to run the query, I am getting an error message that states I have too many expressions in my subquery list and that I can only do that if I start the subquery with 'Exists'. I suppose I do not understand what I am doing wrong, maybe someone could explain how 'Exists' works? Is there a way I can rewrite this so I can return both expressions in the subquery? I will include the syntax and error message below.

SELECT Production.Product.Name
       ,(SELECT Production.Location.Name
         ,Production.Location.Availability
         FROM Production.Location
         WHERE Production.Location.LocationID = Production.ProductInventory.LocationID)
FROM Production.Product
    INNER JOIN Production.ProductInventory
    ON Production.Product.ProductID = Production.ProductInventory.ProductID;

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

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

发布评论

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

评论(1

怪我入戏太深 2025-02-02 07:22:47

您不需要 subquery 这似乎只是一个外部连接:

select p.Name, l.Name Location, l.Availability
from Production.Product p
join Production.ProductInventory i
left join Production.Location l on l.LocationID = i.LocationID
on p.ProductID = i.ProductID;

请注意查询如何通过使用表别名进行整理。

You don't need a subquery here this appears to be just an outer join:

select p.Name, l.Name Location, l.Availability
from Production.Product p
join Production.ProductInventory i
left join Production.Location l on l.LocationID = i.LocationID
on p.ProductID = i.ProductID;

Note how tidier the query is by using table aliases.

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