SQL组合键查询
再次引用这个SQL products/productsales
当主键组成时我怎么能做类似的事情两列而不是一列?
因此 products 有两列作为 PK,productsales 有两列作为 FK。
这是使用 1 列键的解决方案:
SELECT p.[name]
FROM products p
WHERE p.product_id in (SELECT s.product_id
FROM productsales s
WHERE s.[date] between @dateStart and @dateEnd
GROUP BY s.product_id
HAVING Sum(s.quantity) > @X )
to reference this again SQL products/productsales
how could i do something like that when the primary key is made up of two columns and not one?
so products has two columns as PK, and productsales has two columns as FK.
here is the solution with a 1-column key:
SELECT p.[name]
FROM products p
WHERE p.product_id in (SELECT s.product_id
FROM productsales s
WHERE s.[date] between @dateStart and @dateEnd
GROUP BY s.product_id
HAVING Sum(s.quantity) > @X )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
沿着这些思路的东西可能会起作用
Something along these lines could work
也许像这样的事情会做:
Maybe something like this would do:
看起来上面有一些答案是可行的,但只是为了向您抛出另一个解决方案,以防它在您的情况下效果更好:
此方法往往会比 Mr. 给出的带有 GROUP BY 方法的 INNER JOIN 表现更差。 Brownstone,但在某些情况下,根据您的 @cutoff_quantity 值和表大小,它可能会表现得更好。
It looks like there are a few answers above that will work, but just to throw another solution at you in case it works better in your case:
This method will tend to perform worse than the INNER JOIN with a GROUP BY method given by Mr. Brownstone, but in some situations depending on your value of @cutoff_quantity and table sizes it could perform better.
尝试:
Try:
我喜欢这样做:
I like to do it like this:
用一键重构
两个键相同:
Refactored with one key
Same on two keys: