T-SQL-选择表中的条件属性

时间:2019-03-15 23:06:15

标签: sql-server tsql

我在多种条件下搜索时遇到问题。

示例:您要购买具有“红色”和“尺寸10”属性的鞋子。

在我的结果中,我不知道如何删除仅具有正确颜色或大小的鞋子(我的意思是:我只想选择具有所有属性的鞋子)。

Declare @Tbl_Atributes Table
                       (
                            [NameAttribute] nvarchar(250),
                            [ValueAttribute] nvarchar(250)
                       )

Declare @Tbl_Product Table
                     (
                        [Code] int,
                        [Name] nvarchar(250),
                        [NameAttribute] nvarchar(250),
                        [ValueAttribute] nvarchar(250)
                     )

Insert Into @Tbl_Atributes 
values ('Color', 'Red'), ('Size', '10')

Insert Into @Tbl_Product ([Code], [Name], [NameAttribute], [ValueAttribute])
values ('1', 'Nike', 'Color', 'Red'),
       ('1', 'Nike', 'Color', 'Blue'),
       ('1', 'Nike', 'Size', '10'),
       ('2', 'Adidas', 'Size', '10')

我想让产品在NameAttribute中包含所有@Tbl_Atributes

我的第一次尝试:

select tp.*
from @Tbl_Product tp
inner join @Tbl_Atributes as ta on tp.NameAttribute = ta.NameAttribute
                                and tp.ValueAttribute = ta.ValueAttribute

我遇到了问题:产品Adidas具有1个属性名称:“ Size”,并且仍在结果中。 (我们需要2个属性名称:Size和Color,只有Nike拥有全部)

请帮我解决。谢谢。

2 个答案:

答案 0 :(得分:1)

您可以使用EXISTS:

select distinct [Code], [Name] from Tbl_Product t
where
  exists (
    select 1 from Tbl_Product 
    where [Code] = t.[Code] and [NameAttribute] = 'Color' and [ValueAttribute] = 'Red'
  )
  and 
  exists (
    select 1 from Tbl_Product 
    where [Code] = t.[Code] and [NameAttribute] = 'Size' and [ValueAttribute] = '10'
  )

请参见demo
编辑
要从Tbl_Atributes获取具有所有属性的产品:

select distinct [Code], [Name] from Tbl_Product t
where
(select count(distinct [NameAttribute]) from Tbl_Product where [Code] = t.[Code]) =
(select count(distinct [NameAttribute]) from Tbl_Atributes) 

请参见demo
Edit2
为了获得更好的性能,请使用CTE:

with cte as (
  select count(distinct [NameAttribute]) total from Tbl_Atributes
)
select [Code], [Name] from Tbl_Product t
group by [Code], [Name]
having count(distinct [NameAttribute]) = (select total from cte)

请参见demo
如果不存在商品重复属性的情况,可以将count(distinct [NameAttribute])替换为count(*)

答案 1 :(得分:0)

我找到了这样的解决方案;

Declare @Tbl_Atributes Table(
[NameAttribute] nvarchar(250)
,[ValueAttribute] nvarchar(250))

Declare @Tbl_Product Table(
    [Code] int,
    [Name] nvarchar(250),
    [NameAttribute] nvarchar(250)
    ,[ValueAttribute] nvarchar(250))

Insert Into @Tbl_Atributes values ('Color', 'Red'), ('Size', '10')

Insert Into @Tbl_Product  ([Code],[Name], [NameAttribute], [ValueAttribute])
values ('1','Nike','Color','Red'),('1','Nike','Color','Blue'),('1','Nike','Size','10')
        ,('2','Addidas','Size','10')


select tmp.* 
from (select tp1.Code, tp1.Name
        from @Tbl_Product tp1 
        group by tp1.Code,tp1.Name) as tmp    
where (select COUNT(1) from @Tbl_Atributes)=
        (select count(1) 
        from @Tbl_Product tp
        join @Tbl_Atributes ta on tp.NameAttribute=ta.NameAttribute and tp.ValueAttribute=ta.ValueAttribute
        where tp.Code=tmp.Code)