在Eav模型中过滤具有多个动态属性的查询

时间:2018-12-01 10:24:17

标签: sql sql-server entity-attribute-value

我有5张桌子

Advertismenet>我的应用程序中包含一些广告

类别

类别详细信息>用于类别的属性

类别详细信息定义>用于类别详细信息定义

类别详细值>用于提交

我想实现EAV模型。 例如,我有此样本数据。

  

类别

     
      
  • 编号1
  •   
  • 命名手机
  •   
     
    

类别详细信息

         
        
    •     
    • 编号1          
            
      • 名称价格
      •     
      • CategoryId 1
      •     
      • ControlType字符串
      •     
    •     
  1.     
    •     
    • 编号2          
            
      • 名称RAM
      •     
      • CategoryId 1
      •     
      • ControlType DropDown
      •     
    •     
  2.     
    •     
    • 编号3          
            
      • 难名
      •     
      • CategoryId 1
      •     
      • ControlType号
      •     
    •     
  3.     
  

CategoryDe​​tail定义

    • 编号1
      • 名称4G
      • CategoryDe​​tailId 2
    • 编号2
      • 名称8G
      • CategoryDe​​tailId 2
  

Advertismenet

     
      
    •   
    • 编号1      
          
      • 命名三星银河
      •   
      • CategoryId 1
      •   
    •   
  1.   
    •   
    • 编号2      
          
      • 命名三星银河
      •   
      • CategoryId 1
      •   
    •   
  2.   

详细值

    • 编号1
      • AdvertisementId 1
      • DetailId 1
      • 值530000
      • DefinitionId为空
    • 编号1
      • AdvertisementId 1
      • DetailId 2
      • 值为空
      • DefinitionId 1

所以我想拥有一个过滤器页面,用户可以选择他/ s想要的属性。例如,我作为用户想要一个具有8G ram的手机列表,它们是Black,...。

更新:

CREATE TABLE Advertisement_Categories([Id] [bigint] IDENTITY(1,1) NOT NULL)
CREATE TABLE Advertisement_CategoryDetail_Definitions(
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[Advertisement_CategoryDetailId] [bigint] NOT NULL,
[Name] [nvarchar](max) NULL)
CREATE TABLE Advertisement_CategoryDetail_Values(
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[Advertisement_CategoryDetailId] [bigint] NOT NULL,
[AdvertisementId] [bigint] NULL,
[Advertisement_CategoryDetail_DefinitionId] [bigint] NULL,
[Value] [nvarchar](max) NULL)
CREATE TABLE Advertisement_CategoryDetails(
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](max) NULL,
[DynamicUserControlType] [int] NOT NULL,
[Advertisement_CategoryId] [bigint] NOT NULL)

CREATE TABLE Advertisements(
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[Advertisement_CategoryId] [bigint] NOT NULL)

INSERT [dbo].[Advertisement_Categories] ([Id], [CreateDate], [UpdateDate], [IsArchived], [Name], [IconPath], [OrderNumber], [ParentId]) VALUES (4, CAST(N'2018-12-01T07:06:50.2165209+00:00' AS DateTimeOffset), CAST(N'0001-01-01T00:00:00.0000000+00:00' AS DateTimeOffset), 0, N'طلا', NULL, 0, 3)
INSERT [dbo].[Advertisement_CategoryDetail_Definitions] ([Id], [CreateDate], [UpdateDate], [IsArchived], [Advertisement_CategoryDetailId], [Name], [OrderNumber], [Version]) VALUES (1, CAST(N'2018-12-01T07:07:08.8619579+00:00' AS DateTimeOffset), CAST(N'0001-01-01T00:00:00.0000000+00:00' AS DateTimeOffset), 0, 1, N'عیار ۱۸', 0, 636792448288619526)
INSERT [dbo].[Advertisement_CategoryDetail_Definitions] ([Id], [CreateDate], [UpdateDate], [IsArchived], [Advertisement_CategoryDetailId], [Name], [OrderNumber], [Version]) VALUES (2, CAST(N'2018-12-01T07:08:15.2481173+00:00' AS DateTimeOffset), CAST(N'0001-01-01T00:00:00.0000000+00:00' AS DateTimeOffset), 0, 2, N'سفید', 0, 636792448952481159)
INSERT [dbo].[Advertisement_CategoryDetail_Definitions] ([Id], [CreateDate], [UpdateDate], [IsArchived], [Advertisement_CategoryDetailId], [Name], [OrderNumber], [Version]) VALUES (5, CAST(N'2018-12-01T07:07:08.8619579+00:00' AS DateTimeOffset), CAST(N'2018-12-01T07:07:08.8619579+00:00' AS DateTimeOffset), 0, 1, N'عیار 24', 0, 0)
INSERT [dbo].[Advertisement_CategoryDetail_Definitions] ([Id], [CreateDate], [UpdateDate], [IsArchived], [Advertisement_CategoryDetailId], [Name], [OrderNumber], [Version]) VALUES (6, CAST(N'2018-12-01T07:08:15.2481173+00:00' AS DateTimeOffset), CAST(N'2018-12-01T07:08:15.2481173+00:00' AS DateTimeOffset), 0, 2, N'زرد', 0, 0)
INSERT [dbo].[Advertisement_CategoryDetail_Definitions] ([Id], [CreateDate], [UpdateDate], [IsArchived], [Advertisement_CategoryDetailId], [Name], [OrderNumber], [Version]) VALUES (7, CAST(N'2018-12-01T07:08:15.2481173+00:00' AS DateTimeOffset), CAST(N'2018-12-01T07:08:15.2481173+00:00' AS DateTimeOffset), 0, 2, N'سیاه', 0, 0)
INSERT [dbo].[Advertisement_CategoryDetail_Values] ([Id], [CreateDate], [UpdateDate], [IsArchived], [Advertisement_CategoryDetailId], [AdvertisementId], [Advertisement_CategoryDetail_DefinitionId], [Value], [Version]) VALUES (1, CAST(N'2018-12-01T07:15:22.5333139+00:00' AS DateTimeOffset), CAST(N'0001-01-01T00:00:00.0000000+00:00' AS DateTimeOffset), 0, 1, 11, 1, NULL, 636792453225333028)
INSERT [dbo].[Advertisement_CategoryDetail_Values] ([Id], [CreateDate], [UpdateDate], [IsArchived], [Advertisement_CategoryDetailId], [AdvertisementId], [Advertisement_CategoryDetail_DefinitionId], [Value], [Version]) VALUES (2, CAST(N'2018-12-01T07:15:22.5333183+00:00' AS DateTimeOffset), CAST(N'0001-01-01T00:00:00.0000000+00:00' AS DateTimeOffset), 0, 2, 11, 2, NULL, 636792453225333183)
INSERT [dbo].[Advertisement_CategoryDetail_Values] ([Id], [CreateDate], [UpdateDate], [IsArchived], [Advertisement_CategoryDetailId], [AdvertisementId], [Advertisement_CategoryDetail_DefinitionId], [Value], [Version]) VALUES (3, CAST(N'2018-12-01T07:15:22.5333183+00:00' AS DateTimeOffset), CAST(N'2018-12-01T07:15:22.5333183+00:00' AS DateTimeOffset), 0, 2, 10, 6, NULL, 0)
INSERT [dbo].[Advertisement_CategoryDetails] ([Id], [CreateDate], [UpdateDate], [IsArchived], [Name], [IsRequired], [DynamicUserControlType], [OrderNumber], [Advertisement_CategoryId], [Version]) VALUES (1, CAST(N'2018-12-01T07:06:57.5101115+00:00' AS DateTimeOffset), CAST(N'0001-01-01T00:00:00.0000000+00:00' AS DateTimeOffset), 0, N'عیار', 0, 3, 0, 4, 636792448175100972)
INSERT [dbo].[Advertisement_CategoryDetails] ([Id], [CreateDate], [UpdateDate], [IsArchived], [Name], [IsRequired], [DynamicUserControlType], [OrderNumber], [Advertisement_CategoryId], [Version]) VALUES (2, CAST(N'2018-12-01T07:08:04.5508379+00:00' AS DateTimeOffset), CAST(N'0001-01-01T00:00:00.0000000+00:00' AS DateTimeOffset), 0, N'رنگ', 0, 3, 1, 4, 636792448845508373)
INSERT [dbo].[Advertisements] ([Id], [CreateDate], [UpdateDate], [IsArchived], [OwnerUserId], [ProcessState], [SellKind], [Address], [Longitude], [Latitude], [Title], [Price], [DiscountPercent], [Warranty], [Description], [Advertisement_CategoryId], [ProvinceId], [CityId], [PointKind]) VALUES (10, CAST(N'2018-11-28T13:04:29.3749610+00:00' AS DateTimeOffset), CAST(N'0001-01-01T00:00:00.0000000+00:00' AS DateTimeOffset), 0, 1, 0, 0, N'1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA', -122.08400000000002, 37.421998333333335, N'ghfgh', CAST(65465436.00 AS Decimal(18, 2)), 0, NULL, NULL, 2, 1, 1, 2)
INSERT [dbo].[Advertisements] ([Id], [CreateDate], [UpdateDate], [IsArchived], [OwnerUserId], [ProcessState], [SellKind], [Address], [Longitude], [Latitude], [Title], [Price], [DiscountPercent], [Warranty], [Description], [Advertisement_CategoryId], [ProvinceId], [CityId], [PointKind]) VALUES (11, CAST(N'2018-12-01T07:15:20.4668455+00:00' AS DateTimeOffset), CAST(N'0001-01-01T00:00:00.0000000+00:00' AS DateTimeOffset), 0, 1, 0, 0, N'1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA', -122.08400000000002, 37.421998333333335, N'dfgsfdg', CAST(225000000.00 AS Decimal(18, 2)), 0, NULL, NULL, 4, 1, 1, 2)

如何选择带有多个过滤器的广告列表 记住,每个属性都在一行上,所以我不能只使用'and'

1 个答案:

答案 0 :(得分:1)

此查询可能有帮助

select * from Advertisements where 
exists (select AdvertisementId from Advertisement_CategoryDetail_Values where  AdvertisementId = Advertisements.id  and  Advertisement_CategoryDetailId = 1 and Advertisement_CategoryDetail_DefinitionId = 1)
and
exists (select AdvertisementId from Advertisement_CategoryDetail_Values where AdvertisementId = Advertisements.id  and Advertisement_CategoryDetailId = 2 and Advertisement_CategoryDetail_DefinitionId = 2)