优化已取消订单的 SQL 查询
这是我的表的子集:
orders:
- order_id
- customer_id
order_products:
- order_id
- order_product_id (unique key)
- canceled
我想选择给定客户(customer_id)的所有订单(order_id),其中订单中的所有产品都被取消,而不仅仅是某些产品。有没有比这更优雅或更有效的方法:
select order_id from orders
where order_id in (
select order_id from orders
inner join order_products on orders.order_id = order_products.order_id
where order_products.customer_id = 1234 and order_products.canceled = 1
)
and order_id not in (
select order_id from orders
inner join order_products on orders.order_id = order_products.order_id
where order_products.customer_id = 1234 and order_products.canceled = 0
)
Here is a subset of my tables:
orders:
- order_id
- customer_id
order_products:
- order_id
- order_product_id (unique key)
- canceled
I want to select all orders (order_id) for a given customer(customer_id), where ALL of the products in the order are canceled, not just some of the products. Is there a more elegantly or efficient way of doing it than this:
select order_id from orders
where order_id in (
select order_id from orders
inner join order_products on orders.order_id = order_products.order_id
where order_products.customer_id = 1234 and order_products.canceled = 1
)
and order_id not in (
select order_id from orders
inner join order_products on orders.order_id = order_products.order_id
where order_products.customer_id = 1234 and order_products.canceled = 0
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
由于我们不知道数据库平台,这里有一个 ANSI 标准方法。请注意,这对模式没有任何假设(即已取消字段的数据类型、如何设置已取消标志(即“是”、1 等)),并且不使用特定于给定数据库平台的任何内容(这可能是如果您可以向我们提供您正在使用的平台和版本,则更有效的方法):
Since we don't know the database platform, here's an ANSI standard approach. Note that this assumes nothing about the schema (i.e. data type of the cancelled field, how the cancelled flag is set (i.e. 'YES',1,etc.)) and uses nothing specific to a given database platform (which would likely be a more efficient approach if you could give us the platform and version you are using):
如果所有订单在 order_products 中至少有一行,请尝试一下
如果上述假设不成立,那么您还需要:
If all orders have at least one row in order_products, Try this
If the above assumption is not true, then you also need:
最快的方法是这样的:
当且仅当存在一些产品并且它们全部被取消时,子查询才会返回
0
。如果根本没有产品,子查询将返回
NULL
;如果至少有一个未取消的产品,子查询将返回1
。确保您在
order_products(order_id,已取消)
上有索引The fastest way will be this:
The subquery will return
0
if and only if there had been some products and they all had been canceled.If there were no products at all, the subquery will return
NULL
; if there is at least one uncanceled product, the subquery will return1
.Make sure you have an index on
order_products (order_id, canceled)
像这样的东西吗?这假设每个订单至少有一种产品,否则此查询还将返回没有任何产品的订单。
Something like this? This assumes that every order has at least one product, otherwise this query will return also orders without any products.
你可以尝试这样的事情
You can try something like this