Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL从3个表创建视图_Sql_Sql Server_View - Fatal编程技术网

SQL从3个表创建视图

SQL从3个表创建视图,sql,sql-server,view,Sql,Sql Server,View,我有三个表:产品、属性和属性定义 产品: +----+------+ |Id |名称| +----+------+ |1 |鞋1| +----+------+ |2 |鞋2| +----+------+ |3 | Shoe3| +----+------+ 属性定义: +----+---------+ |Id |名称| +----+---------+ |1 |颜色| |2 |型| |3 |命运| +----+---------+ 属性: +----+--------------+------

我有三个表:产品、属性和属性定义

产品:

+----+------+
|Id |名称|
+----+------+
|1 |鞋1|
+----+------+
|2 |鞋2|
+----+------+
|3 | Shoe3|
+----+------+

属性定义:

+----+---------+
|Id |名称|
+----+---------+
|1 |颜色|
|2 |型|
|3 |命运|
+----+---------+

属性:

+----+--------------+--------------+-----------+
|Id |值|定义Id |产品Id|
+----+--------------+--------------+-----------+
|1 |棕色| 1 | 2|
|2 |黄色| 1 | 1|
|3 |体育| 3 | 1|
|4 |果冻鞋| 2 | 1|
|5 |普通鞋| 2 | 2|
+----+--------------+--------------+-----------+

在AttributesDefinitions中,我需要属性定义

在属性中,我有属性及其值

每个产品都有许多属性,但每种类型只有一个(属性定义)

我的任务是创建一个包含产品列表及其所有属性值的视图。 应该是这样的:

产品属性视图:


+---------+--------+--------------+---------+
|产品|颜色|类型|命运|
+---------+--------+--------------+---------+
|鞋1 |黄色|果冻鞋|运动鞋|
|鞋2 |棕色|普通鞋|空|
|Shoe3 | NULL | NULL | NULL|
+---------+--------+--------------+---------+
其目的是获取B2B平台上的产品列表,并能够通过属性值对其进行筛选


有什么帮助吗?

如我在评论中所述,我强烈不同意您描述的模式,因为在我看来,Products表应该与属性具有一对多或多对多的关系。换句话说,一个产品可以有许多属性,而许多产品可以共享相同的属性。此外,由于您提到的设计以及您的最终目标(具有任意长度属性列表作为列的产品),您的要求可能不可能实现。您可以在视图定义中使用精心编制的PIVOT语句来实现这些结果。首先让我知道下面的代码是否有效

综上所述,如果我假设您使用的是T-SQL,那么这段代码将创建您描述的表,并创建一个基于视图的表,在表上执行连接。希望这能推动你朝着正确的方向前进

CREATE TABLE Products
(
Id INT PRIMARY KEY,
Name VARCHAR(255)
);

CREATE TABLE AttributesDefinition
(
Id INT PRIMARY KEY,
Name VARCHAR(255)
);

CREATE TABLE Attributes
(
Id INT,
Name VARCHAR(255),
Value VARCHAR(255),
DefinitionId INT FOREIGN KEY REFERENCES AttributesDefinition (Id),
ProductId INT FOREIGN KEY REFERENCES Products (Id)
);


CREATE VIEW ProductsWithAttributesView AS
SELECT p.Name AS Products
,ad.Name AS AttributeDefinition
,a.Name AS AttributeName
,a.Value AS AttributeValue
FROM Products p 
INNER JOIN Attributes a ON p.Id = a.ProductId
INNER JOIN AttributesDefinition ad ON ad.Id = a.DefinitionId;

SELECT * FROM ProductsWithAttributesView;

我使用CaitLAN Jenner的代码来展示我的解决方案,但我不确定您是否可以创建一个视图,该视图将在添加新类别时动态调整

CREATE TABLE Products
(
Id INT PRIMARY KEY,
Name VARCHAR(255)
);

INSERT INTO Products (Id,Name)
VALUES (1,'Car'),(2,'Motorcycle'),(3,'Bicycle')

CREATE TABLE AttributesDefinition
(
Id INT PRIMARY KEY,
Name VARCHAR(255)
);

INSERT INTO AttributesDefinition (Id,Name)
VALUES (1,'Number of wheels'),(2,'People'),(3,'Engine')

CREATE TABLE Attributes
(
Id INT,
Name VARCHAR(255),
Value VARCHAR(255),
DefinitionId INT FOREIGN KEY REFERENCES AttributesDefinition (Id),
ProductId INT FOREIGN KEY REFERENCES Products (Id)
);

INSERT INTO Attributes (Id, Name, Value, DefinitionId, ProductId)
VALUES (1,'Number of wheels','4',1,1),
 (2,'Number of wheels','2',1,2),
 (3,'Number of wheels','2',1,3),
 (4,'People','4',2,1),
 (5,'People','2',2,2),
 (6,'People','1',2,3),
 (7,'Engine','V6',3,1),
 (8,'Engine','V2',3,2)


CREATE VIEW ProductsWithAttributesView AS
SELECT      
    products.Name as 'Products',
    atr1.Value As 'Number of wheels',
    atr2.Value As 'People',
    atr3.Value As 'Engine'
FROM Products AS products
LEFT JOIN Attributes AS atr1 ON atr1.ProductId = products.Id AND atr1.DefinitionId = 1
LEFT JOIN Attributes AS atr2 ON atr2.ProductId = products.Id AND atr2.DefinitionId = 2
LEFT JOIN Attributes AS atr3 ON atr3.ProductId = products.Id AND atr3.DefinitionId = 3
结果

Products  | Number of wheels |  People | Engine
Car       |  4               |   4     | V6
Motorcycle|  2               |   2     | V2
Bicycle   |  2               |   1     | NULL
编辑。这个有枢轴的:

CREATE VIEW ProductsWithAttributesView2 AS
WITH pivot_data AS
(
SELECT products.Name as Products, -- Grouping Column
def.Name as AtrName, -- Spreading Column
Value -- Aggregate Column
FROM Attributes atr
INNER JOIN Products products ON atr.ProductId = products.Id 
INNER JOIN AttributesDefinition def ON def.Id = atr.DefinitionId 
)
SELECT Products, [Number of wheels],[People],[Engine]
FROM pivot_data
PIVOT (max(value) FOR AtrName IN ([Number of wheels],[People],[Engine])) AS p;

到目前为止,您尝试了什么?您使用的是什么rdbms?问题是SQL,所以我先看看SQL连接。检查这个,我在MS SQL上工作。对于数据库设计我绝对无能为力。我添加了这个任务的目的。也许还有另一个更好的方法来解决这个问题。我以前没有关于枢轴的经验,我正在挖掘这个主题。我用更多的信息编辑了我的第一篇文章。