子查询自动产生交叉连接

发布于 2024-11-03 21:09:00 字数 874 浏览 5 评论 0原文

我不确定为什么,但是当我按照示例(来自 ms sql 服务器中的 Northwind 数据集)通过输入如下所示的代码在 Microsoft SQL Server Management Studio 2008 上执行子查询时,

Select Orders.OrderID, 
       (Select Customers.CompanyName 
          From Customers 
          Where Customers.CustomerID = Orders.CustomerID) As Company Name
  From Orders, 
       Customers

带有子查询的 SQL 代码自动获得了交叉连接并成为

    Select Orders.OrderID,
           (Select Customers.CompanyName 
              From Customers
             Where Customers.CustomerID = Orders.CustomerID) As Company Name
      From Orders 
CROSS JOIN Customers as Customers_1

我已经尝试了几种变体,但没有成功消除这个问题。这是 Microsoft SQL Server Management Studio 2008 的已知错误吗?如果有的话,是否已经打过补丁,如何找到打过补丁的?否则,我如何向 Microsoft 报告此问题并让他们真正快速修复它?

在我的实际查询中,我需要通过使 ID 相等来查询/查找这个特定表的名称大约 50 次,我认为为此执行任何类型的 JOIN 简直是愚蠢的,因为代码很糟糕,非常长,并且性能可能很差?

I am not certain WHY but when I follow the example (from the northwind datase in a ms sql server) to do a subquery on Microsoft SQL Server Management Studio 2008 by typing in the code like shown below,

Select Orders.OrderID, 
       (Select Customers.CompanyName 
          From Customers 
          Where Customers.CustomerID = Orders.CustomerID) As Company Name
  From Orders, 
       Customers

This sql code with subquery automatically gained a cross join and become

    Select Orders.OrderID,
           (Select Customers.CompanyName 
              From Customers
             Where Customers.CustomerID = Orders.CustomerID) As Company Name
      From Orders 
CROSS JOIN Customers as Customers_1

I have played around with several variation of this but with no luck in eliminating this problem. Is this a known bug for microsoft sql server management studio 2008? If so, has it been patched, how do I find the patched? Otherwise, how can I report this to Microsoft and get them to really fixed it quickly?

In my actual query, I need to query/lookup the name of this particular table about 50 times by equating the ID and I think it is simply dumb having to do a JOIN of any sort for this because the code is crumpy, VERY long, and performance may be poor?

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

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

发布评论

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

评论(3

一抹淡然 2024-11-10 21:09:00

子查询不会导致交叉连接,而是缺少控制连接的条件。你需要这样的东西:

Select Orders.OrderID, (Select Customers.CompanyName From Customers Where Customers.CustomerID = Orders.CustomerID) As Company Name 
From Orders, Customers
Where Orders.CustomerID = Customers.CustomerID

The subquery isn't causing the cross join, the lack of a condition controlling the join is. You need something like this:

Select Orders.OrderID, (Select Customers.CompanyName From Customers Where Customers.CustomerID = Orders.CustomerID) As Company Name 
From Orders, Customers
Where Orders.CustomerID = Customers.CustomerID
难理解 2024-11-10 21:09:00

我不知道为什么你的书建议使用子查询——我会这样做:

Select Orders.OrderID, Customers.CompanyName 
  From Orders 
  left join Customers on Customers.CustomerID = Orders.CustomerID

I don't know why a sub-query is suggested by your book -- I would do it like this:

Select Orders.OrderID, Customers.CompanyName 
  From Orders 
  left join Customers on Customers.CustomerID = Orders.CustomerID
半葬歌 2024-11-10 21:09:00

看起来它应该是一个相关子查询

Select Orders.OrderID,

   (Select Customers.CompanyName 
      From Customers 
      Where **Customers.CustomerID = Orders.CustomerID**) As Company Name

From Orders

--,
-- Customers

当内部相关子查询为每个处理的订单带来客户名称时,为什么您会再次需要 Customers 呢?

Management Studio 坚持添加 CROSS JOIN 是在警告您正在做一些奇怪的事情。尝试查询两个表:Customer,Orders,没有任何连接条件。

此外,查询优化器通常会在处理过程中将这些相关子查询转换为联接,但您可以在适当的情况下使用更清晰的语法。

放在哪里合适呢?特别是如果您需要在内部查询上生成某种聚合。

Looks like it should be a correlated-subquery

Select Orders.OrderID,

   (Select Customers.CompanyName 
      From Customers 
      Where **Customers.CustomerID = Orders.CustomerID**) As Company Name

From Orders

--,
-- Customers

Why would you need Customers again when the inner Correlated Subquery brings the customer Name for each Order that is processed?

The Management Studio's insistence on adding CROSS JOIN is a warning that you are doing something strange. Trying to query two tables: Customer,Orders without any join condition.

Also, the query optimizer will usually convert these correlated sub-queries into joins during processing, but you can use the clearer syntax where appropriate.

Where is it appropriate? Particularly if you need to generate some sort of aggregate on the inner query.

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