Select 在不同的两行中具有一定价值的所有零件号
Select All Part Numbers that Have Some Value in Two Different Rows
如果我查询:
SELECT PartNumber FROM Product_Options;
我得到这样的结果:
我真正需要做的是 select 全部来自 Product_Options,其中 OptionName = 'Category' AND OptionValue = 'KID',OptionName = 'Category' AND OptionName = 'SKT'。 OptionName 必须在同一行中具有这些值。
我现在可能离这里很远了,但我尝试的查询导致语法错误:
SELECT PartNumber FROM Product_Options WHERE OptionName = 'Category' AND OptionValue = 'KID', OptionName = 'Category' AND OptionValue = 'SKT';
如果它确实有效,上面的查询将 return 'CR5661' 并且其他 PartNumbers 的 OptionValue 为 KID 和 OptionValue 为 SKT
我不确定是否需要使用 JOIN,但我的想法是 select 类别为 KID 且类别也是 SKT 的所有部件号都具有相同的部件号。
如果 OptionValue
对于每个 PartNumber
都是唯一的,您可以使用 count(distinct OptionValue)
或 count(*)
,并使用 where
将源限制为仅值你在找
select PartNumber
from Product_Options
where OptionName = 'Category'
and (OptionValue = 'KID' or OptionValue = 'SKT')
group by PartNumber
having count(distinct OptionValue)>1
如果我查询:
SELECT PartNumber FROM Product_Options;
我得到这样的结果:
我真正需要做的是 select 全部来自 Product_Options,其中 OptionName = 'Category' AND OptionValue = 'KID',OptionName = 'Category' AND OptionName = 'SKT'。 OptionName 必须在同一行中具有这些值。
我现在可能离这里很远了,但我尝试的查询导致语法错误:
SELECT PartNumber FROM Product_Options WHERE OptionName = 'Category' AND OptionValue = 'KID', OptionName = 'Category' AND OptionValue = 'SKT';
如果它确实有效,上面的查询将 return 'CR5661' 并且其他 PartNumbers 的 OptionValue 为 KID 和 OptionValue 为 SKT
我不确定是否需要使用 JOIN,但我的想法是 select 类别为 KID 且类别也是 SKT 的所有部件号都具有相同的部件号。
如果 OptionValue
对于每个 PartNumber
都是唯一的,您可以使用 count(distinct OptionValue)
或 count(*)
,并使用 where
将源限制为仅值你在找
select PartNumber
from Product_Options
where OptionName = 'Category'
and (OptionValue = 'KID' or OptionValue = 'SKT')
group by PartNumber
having count(distinct OptionValue)>1