需要有关 SQL 子查询的帮助
我正在尝试编写一个查询,该查询将返回仅包含订阅的所有订单。编写一个包含所有带订阅的订单的查询,另一个包含所有不带订阅的订单的查询,然后将它们与不匹配的查询进行比较是很容易的。
但我不想将查询存储在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设
订阅。
仅订阅,无其他
产品类型。
像这样的事情怎么样:
Assumptions
subscriptions.
only subscriptions and no other
product types.
How about something like this:
如果您不想担心连接问题,可以使用数据透视表来实现此目的。
If you do not want to worry about having a join, here is a way to do it with a pivot table.