sql - TSQL:给定一组 ID,获取所有标头,使得该集包含在标头的子项中
问题描述
我正在寻找一个查询,该查询将返回包含一组产品 ID 的所有订单 ID,但可能包含更多产品。
数据结构:
CREATE TABLE dbo.Order(OrderID int, OrderDesc nvarchar(100))
CREATE TABLE dbo.OrderDetail(DetailID int, OrderID int, ProductID int)
CREATE TABLE dbo.Product(ProductID INT, ProductDesc nvarchar(100))
因此,给定一组 productID,我想要一个查询来返回包含该集合中所有(可能更多)产品的所有订单。
解决方案
您可以使用聚合:
select od.orderid
from orderdetail od
where od.productId in (. . .)
group by od.orderid
having count(od.productid) = <n>; -- <n> is the number of products in the list
这假设订单中没有重复的产品。如果可能,请使用count(distinct od.productid)
.
推荐阅读
- flutter - Flutter Flame SpriteComponent with Tapable 无法识别 onTapDown 方法
- html - 浏览器可以原生播放打包的字幕(包含在视频文件中)吗?
- python - 在 Python 中用多维数组实现 Matlab 的 Parfor
- javascript - 我正在尝试使用 Aurelia 和 JQuery 在列表更新时滚动到列表底部
- go - 从 float64 秒创建 time.Duration
- laravel - 从 Laravel Sail Dockerfile 添加到 PATH
- flutter - 颤振:谷歌地图颤振:如何动态改变圆半径?
- python - discord.py 中的子机器人生成
- android - 进行颤振运行时,资产图像未显示在 FLutter Web 中 - 发布
- javascript - 有没有更好的方法来编写这个函数来生成随机名称?