需要有关 SQL 子查询的帮助

发布于 2024-08-24 18:39:38 字数 1051 浏览 3 评论 0原文

我正在尝试编写一个查询,该查询将返回仅包含订阅的所有订单。编写一个包含所有带订阅的订单的查询,另一个包含所有不带订阅的订单的查询,然后将它们与不匹配的查询进行比较是很容易的。

但我不想将查询存储在我的 Access 数据库中,我更喜欢将其全部存储在我的 ASP 代码中,而且我无法仅使用一个复杂的查询来使其工作。

以下是存储它们时有效的示例:

Query1

SELECT tblOrders.OrderID, tblOrderItems.ProductID
FROM tblOrders INNER JOIN tblOrderItems ON tblOrders.OrderID = tblOrderItems.OrderID
WHERE ((Not ((tblOrderItems.ProductID)>=12 And (tblOrderItems.ProductID)<=15)));

Query2

SELECT tblOrders.OrderID, tblOrderItems.ProductID
FROM tblOrders INNER JOIN tblOrderItems ON tblOrders.OrderID = tblOrderItems.OrderID
WHERE ((((tblOrderItems.ProductID)>=12 And (tblOrderItems.ProductID)<=15)));

Query3

SELECT Query2.OrderID, Query2.ProductID
FROM Query2 LEFT JOIN Query1 ON Query2.OrderID = Query1.OrderID
WHERE (((Query1.OrderID) Is Null));

因此,我的问题是“如何编写 Query3,使其不引用 Query1 或 Query2?” 或者,我是否缺少其他方法来做到这一点?

谢谢, 皮特 [电子邮件受保护]

I am trying to write a query that will return all orders that only have a Subscription included. It is easy enough to write a query that includes all Orders with Subscriptions, another that includes all orders without a Subscription and then compare them with an unmatched query.

But I don't want to have to store Queries in my Access database, I prefer to have it all in my ASP code, and I can't get this to work with just one complex query.

Here are samples of what works if I store them:

Query1

SELECT tblOrders.OrderID, tblOrderItems.ProductID
FROM tblOrders INNER JOIN tblOrderItems ON tblOrders.OrderID = tblOrderItems.OrderID
WHERE ((Not ((tblOrderItems.ProductID)>=12 And (tblOrderItems.ProductID)<=15)));

Query2

SELECT tblOrders.OrderID, tblOrderItems.ProductID
FROM tblOrders INNER JOIN tblOrderItems ON tblOrders.OrderID = tblOrderItems.OrderID
WHERE ((((tblOrderItems.ProductID)>=12 And (tblOrderItems.ProductID)<=15)));

Query3

SELECT Query2.OrderID, Query2.ProductID
FROM Query2 LEFT JOIN Query1 ON Query2.OrderID = Query1.OrderID
WHERE (((Query1.OrderID) Is Null));

So, my question is 'how do I write Query3 so that it doesn't refer to Query1 or Query2?'
or, am I missing some other way do do this?

Thanks,
Pete
[email protected]

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

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

发布评论

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

评论(2

夢归不見 2024-08-31 18:39:38

假设

  • ProductID在12到15之间指的是
    订阅。
  • 您正在寻找所有订单
    仅订阅,无其他
    产品类型。

像这样的事情怎么样:

SELECT O.OrderID, TOI.ProductID
FROM tblOrders O
      INNER JOIN tblOrderItems TOI ON (O.OrderID = TOI.OrderID)
WHERE (TOI.ProductID between 12 and 15) AND
      NOT EXISTS (SELECT * 
                  FROM tblOrderItems TOI2
                  WHERE (NOT TOI2.ProductID between 12 and 15) AND
                        (TOI2.OrderID=O.OrderID)
                 )

Assumptions

  • ProductID between 12 and 15 refers to
    subscriptions.
  • You are looking for all orders with
    only subscriptions and no other
    product types.

How about something like this:

SELECT O.OrderID, TOI.ProductID
FROM tblOrders O
      INNER JOIN tblOrderItems TOI ON (O.OrderID = TOI.OrderID)
WHERE (TOI.ProductID between 12 and 15) AND
      NOT EXISTS (SELECT * 
                  FROM tblOrderItems TOI2
                  WHERE (NOT TOI2.ProductID between 12 and 15) AND
                        (TOI2.OrderID=O.OrderID)
                 )
烈酒灼喉 2024-08-31 18:39:38

如果您不想担心连接问题,可以使用数据透视表来实现此目的。

     select OrderID, 
     sum(case productID between 12 and 15 then 1 else 0 end) HAS_SUBSCRIPTION,
     sum(case productID between 12 and 15 then 0 else 1 end) HAS_OTHER
     FROM tblOrderItems
     GROUP BY OrderID
     HAVING HAS_SUBSCRIPTION > 0 and HAS_OTHER = 0;

If you do not want to worry about having a join, here is a way to do it with a pivot table.

     select OrderID, 
     sum(case productID between 12 and 15 then 1 else 0 end) HAS_SUBSCRIPTION,
     sum(case productID between 12 and 15 then 0 else 1 end) HAS_OTHER
     FROM tblOrderItems
     GROUP BY OrderID
     HAVING HAS_SUBSCRIPTION > 0 and HAS_OTHER = 0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文