gpt4 book ai didi

sql - 从sql server 2008中的多个值中选择值

转载 作者:行者123 更新时间:2023-12-02 05:16:32 25 4
gpt4 key购买 nike

DECLARE @ProductFeature TABLE (ProductID int, FeatureID int)

INSERT INTO @ProductFeature
SELECT 1,100
UNION ALL
SELECT 1,101
UNION ALL
SELECT 1,102
UNION ALL
SELECT 2,103
UNION ALL
SELECT 2,104
UNION ALL
SELECT 3,100
UNION ALL
SELECT 3,101
UNION ALL
SELECT 3,102
UNION ALL
SELECT 4,102
UNION ALL
SELECT 4,101
UNION ALL
SELECT 5,110
UNION ALL
SELECT 5,100
UNION ALL
SELECT 5,101

我的要求是如果我通过 ProductID = 1 , 那么我必须选择 Product具有类似于 ProductID = 1 的功能.

ProductID = 1有 3 个特征(100,101,102),只有 ProductID = 3它具有相同的计数和功能,具有 ProductID = 1

预期结果

ProductID   FeatureID
3 100
3 101
3 102

最佳答案

带有 EXCEPT 的选项操作

DECLARE @ProductID int = 1

SELECT ProductID, FeatureID
FROM ProductFeature p1
WHERE p1.ProductID != @ProductID AND
NOT EXISTS (
SELECT p2.FeatureID
FROM ProductFeature p2
WHERE p2.ProductID = @ProductID
EXCEPT
SELECT p3.FeatureID
FROM ProductFeature p3
WHERE p3.ProductID = p1.ProductID
)

关于sql - 从sql server 2008中的多个值中选择值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14556968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com