-
Notifications
You must be signed in to change notification settings - Fork 0
/
productsCategories.sql
43 lines (36 loc) · 1.3 KB
/
productsCategories.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*SQL*/
CREATE TABLE [dbo].[PRODUCTS] (
[PRODUCT_ID] int IDENTITY(1,1) NOT NULL,
[NAME] nvarchar(255),
PRIMARY KEY CLUSTERED ([PRODUCT_ID])
);
CREATE TABLE [dbo].[CATEGORIES] (
[CATEGORY_ID] int IDENTITY(1,1) NOT NULL,
[NAME] nvarchar(255),
PRIMARY KEY CLUSTERED ([CATEGORY_ID])
);
CREATE TABLE [dbo].[CATEGORY_PRODUCT] (
[CATEGORY_ID] int NOT NULL,
[PRODUCT_ID] int NOT NULL
);
INSERT INTO [dbo].[PRODUCTS] ([NAME])
VALUES ('Кроссовки'),('Ботинки'),('Мяч'),('Плеер');
--ID: 1,2,3,4
INSERT INTO [dbo].[CATEGORIES] ([NAME])
VALUES ('Обувь'),('Спорт');
--ID: 1,2
INSERT INTO [dbo].[CATEGORY_PRODUCT]
VALUES (1,1) --Кроссовки, Обувь
,(1,2) --Кроссовки, Спорт
,(2,1) --Ботинки, Обувь
,(3,2) --Мяч, Спорт
;
--Плеер не определен ни в одну из категорий
SELECT
[P].[NAME] as [PRODUCT_NAME]
,ISNULL([C].[NAME],'<нет категорий>') as [CATEGORY_NAME]
FROM [dbo].[PRODUCTS] as [P]
LEFT JOIN [dbo].[CATEGORY_PRODUCT] as [CP]
ON [CP].[PRODUCT_ID] = [P].[PRODUCT_ID]
LEFT JOIN [dbo].[CATEGORIES] as [C]
ON [C].[CATEGORY_ID] = [CP].[CATEGORY_ID]