Фильтрация запросов с несколькими динамическими свойствами в Eav Model - PullRequest
0 голосов
/ 01 декабря 2018

У меня есть 5 таблиц

Advertismenet> есть несколько объявлений в моем приложении

категория

категория Подробнее> для свойств категории

определения категории подробности>для подробных определений категорий

Значение подробностей категории> для отправки

Я хочу реализовать модель EAV.например, у меня есть этот пример данных.

Категория

  • Идентификатор 1
  • Имя Мобильный

КатегорияДеталь

    • Id 1
      • Имя Цена
      • CategoryId 1
      • ControlType String
    • Id 2
      • Имя RAM
      • CategoryId 1
      • ControlType DropDown
    • Id 3
      • Имя Hard
      • CategoryId 1
      • Номер типа управления

Категория Определение детали

    • Id 1
      • Имя 4G
      • CategoryDetailId 2
    • Id 2
      • Имя 8G
      • CategoryDetailId 2

Advertismenet

    • Id 1
      • Имя Samsung Galaxy
      • CategoryId 1
    • Id 2
      • Имя samsung galaxy
      • CategoryId 1

Детальное значение

    • Id 1
      • AdvertisingId 1
      • DetailId 1
      • Значение 530000
      • DefinitionId null
    • Id 1
      • AdvertisingId 1
      • DetailId 2
      • Значение null
      • DefinitionId 1

Так что я хочу иметь страницу фильтра для этого пользователяможете выбрать, какой атрибут он хочет.Например, я, как пользователь, хочу получить список мобильных телефонов с 8G оперативной памяти, и они черные, ....

Обновление:

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)

Как выбрать список рекламных объявлений с несколькими фильтрами, помните, что каждый атрибут находится в одной строке, поэтому я не могу использовать только 'и'

1 Ответ

0 голосов
/ 04 декабря 2018

Этот запрос может быть полезен

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)
...