我们可以 运行 多次子查询外部查询吗
Can we run subquery multiple time for outer query
我会尽力解释我想要什么。如果你能建议我一个更好的问题标题,那也很好。
在我的存储过程中,我正在传递一个包含 AttributeId
和 AttributeValueId
的 user-defined table,我想检索 MappedAttributeId
user-defined table
的基础
例如我想 select 值 MappedAttributeId = 99
因为在 user-defined table 我通过
AttributeId AttributeValueId
------------------------------
84 156
85 158
我在 table 之间使用连接,我在上面附加了我的 user-defined table 但它会 return 我对所有 attributeid 84 的多个结果,其中 AttributeValueId 是 156 和对于所有 attributeid 85,其中 AttributeValue 为 156
但我想要 MappedAttributeId where AttributeId = 84 AttributeValueId = 156 and AttributeId = 85 AttributeValueId = 158
我想在外部查询中使用 MappedAttributeId 并且我在子查询中使用连接所以无论如何都可以在不使用游标的情况下编写此查询。
好的,这是我传递给存储过程的 user-defined table:
declare @p4 [Order].ProductSelectedAttributes
insert into @p4 values(84,156)
insert into @p4 values(85,158)
以上参数第一个值为AttributeId
,第二个为AttributeValueId
我写的查询是
Select MappedAttributeId
from [Product].tblMappedAttributesDetail mad -- This is the table in the first picture on top
join @p4 psa on psa.attrId = mad.AttributeId and psa.attrValId = mad.AttributeValueId
此查询 returning 三个值 99,99,100,因为我的参数 (84,156) 中的第一行与 table 中的第一行匹配,其中 MappedAttributeId 为 99,第三行其中 MappedAttributeId 为 100,我的参数 (85,158) 中的第二行与 MappedAttributeId 为 100 的 table 中的第二行匹配。我可以使用 GroupBy 删除 重复的 99 值但是如何从结果中排除 100?
但我只想要 MappedAttributeId 99,因为它匹配我的参数 (84,156) 和 (85,158) 的两行。我不想要 MappedAttributeId 100 因为它只匹配 (85,158) 而不是另一行 (84,156)
我刚刚添加了 group by 并使用 Having 进行计数并且它起作用了。感谢大家的帮助。
Select MappedAttributeId
from [Product].tblMappedAttributesDetail mad
join @p4 psa on psa.attrId = mad.AttributeId and psa.attrValId = mad.AttributeValueId
group by MappedAttributeId
Having (Select Count(1) from @p4) = Count(1)
我会尽力解释我想要什么。如果你能建议我一个更好的问题标题,那也很好。
在我的存储过程中,我正在传递一个包含 AttributeId
和 AttributeValueId
的 user-defined table,我想检索 MappedAttributeId
user-defined table
例如我想 select 值 MappedAttributeId = 99
因为在 user-defined table 我通过
AttributeId AttributeValueId
------------------------------
84 156
85 158
我在 table 之间使用连接,我在上面附加了我的 user-defined table 但它会 return 我对所有 attributeid 84 的多个结果,其中 AttributeValueId 是 156 和对于所有 attributeid 85,其中 AttributeValue 为 156
但我想要 MappedAttributeId where AttributeId = 84 AttributeValueId = 156 and AttributeId = 85 AttributeValueId = 158
我想在外部查询中使用 MappedAttributeId 并且我在子查询中使用连接所以无论如何都可以在不使用游标的情况下编写此查询。
好的,这是我传递给存储过程的 user-defined table:
declare @p4 [Order].ProductSelectedAttributes
insert into @p4 values(84,156)
insert into @p4 values(85,158)
以上参数第一个值为AttributeId
,第二个为AttributeValueId
我写的查询是
Select MappedAttributeId
from [Product].tblMappedAttributesDetail mad -- This is the table in the first picture on top
join @p4 psa on psa.attrId = mad.AttributeId and psa.attrValId = mad.AttributeValueId
此查询 returning 三个值 99,99,100,因为我的参数 (84,156) 中的第一行与 table 中的第一行匹配,其中 MappedAttributeId 为 99,第三行其中 MappedAttributeId 为 100,我的参数 (85,158) 中的第二行与 MappedAttributeId 为 100 的 table 中的第二行匹配。我可以使用 GroupBy 删除 重复的 99 值但是如何从结果中排除 100?
但我只想要 MappedAttributeId 99,因为它匹配我的参数 (84,156) 和 (85,158) 的两行。我不想要 MappedAttributeId 100 因为它只匹配 (85,158) 而不是另一行 (84,156)
我刚刚添加了 group by 并使用 Having 进行计数并且它起作用了。感谢大家的帮助。
Select MappedAttributeId
from [Product].tblMappedAttributesDetail mad
join @p4 psa on psa.attrId = mad.AttributeId and psa.attrValId = mad.AttributeValueId
group by MappedAttributeId
Having (Select Count(1) from @p4) = Count(1)