Transact-SQL 查询/如何组合多个 JOIN 语句?
几个小时以来我一直在思考这个 SQL 难题,所以我心里想:“嘿,为什么不向堆栈询问并让网络从该解决方案中受益呢?” em>
就是这样。首先,这些是我的 SQL 表:
Fields
FieldID INT (PK)
FieldName NVARCHAR(50) (IX)
FormFields
FieldID INT (FK)
FormID INT (FK)
Values
FieldID INT (FK)
RecordID INT (FK)
Value NVARCHAR(1000)
Forms
FormID INT (PK)
FormName NVARCHAR(50) (IX)
Records
RecordID INT (PK)
FormID INT (FK)
PoolID INT (FK)
< strong>数据池
PoolID INT (PK)
FormID INT (FK)
PoolName NVARCHAR(50) (IX)
考虑以下约束。
- 每个Form有0个或多个DataPool。每个DataPool只能分配给一个Form。
- 每个表单有 0 个或多个字段。每个字段可能分配给多个表单。
- 每条记录都有 0 个或多个值。每个值都链接到一个记录。
- 每个DataPool有0个或多个Record。每个记录都链接到一个数据池。
- 每个值都链接到一个字段。
- 此外,所有名称列都有唯一的值。
现在,问题是:
我需要根据以下列从值表中查询每个值:
- 链接到值的字段名称
- 链接到值的记录的数据池名称
- 链接到该数据池的表单名称
上面的 3 列必须等于存储过程中接收到的 3 个参数。
这是我到目前为止所得到的:
CREATE PROCEDURE [GetValues]
@FieldName NVARCHAR(50),
@FormName NVARCHAR(50),
@PoolName NVARCHAR(50)
AS SELECT Value FROM [Values]
JOIN [Fields]
ON [Fields].FieldID = [Values].FieldID
WHERE [Fields].FieldName = @FieldName
如何通过 PoolName 列过滤 Values 表的行? DataPools 表不直接与 Values 表相关,但它仍然与 Records 表相关,而 Records 表又与 Values 表直接相关。关于如何做到这一点有什么想法吗?
I'm banging my head on this SQL puzzle since a couple of hours already, so i thought to myself : "Hey, why don't you ask the Stack and allow the web to benefit from the solution?"
So here it is. First thing, these are my SQL tables:
Fields
FieldID INT (PK)
FieldName NVARCHAR(50) (IX)
FormFields
FieldID INT (FK)
FormID INT (FK)
Values
FieldID INT (FK)
RecordID INT (FK)
Value NVARCHAR(1000)
Forms
FormID INT (PK)
FormName NVARCHAR(50) (IX)
Records
RecordID INT (PK)
FormID INT (FK)
PoolID INT (FK)
DataPools
PoolID INT (PK)
FormID INT (FK)
PoolName NVARCHAR(50) (IX)
Consider the following constraints.
- Each Form has 0 or more DataPool. Each DataPool can only be assigned to one Form.
- Each Form has 0 or more Field. Each Field might be assigned to several Form.
- Each Record has 0 or more Value. Each Value is linked to a single Record.
- Each DataPool has 0 or more Record. Each Record is linked to a single DataPool.
- Each Value is linked to one Field.
- Also, all the Name columns have unique values.
Now, here's the problem:
I need to query evey value form the Values table based on the following columns:
- The Name of the Field linked to the Value
- The Name of the DataPool linked the Record linked to the Value
- The Name of the Form linked to that DataPool
The 3 columns above must be equal to the 3 received parameters in the stored procedure.
Here's what I got so far:
CREATE PROCEDURE [GetValues]
@FieldName NVARCHAR(50),
@FormName NVARCHAR(50),
@PoolName NVARCHAR(50)
AS SELECT Value FROM [Values]
JOIN [Fields]
ON [Fields].FieldID = [Values].FieldID
WHERE [Fields].FieldName = @FieldName
How can I filter the rows of the Values table by the PoolName column? The DataPools table isn't directly related to the Values table, but it's still related to the Records table which is directly related to the Values table. Any ideas on how to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我觉得你的问题中我遗漏了一些东西。如果此解决方案不能解决问题,请告诉我哪里缺少问题。
I feel like I am missing something in your question. If this solution is not addressing the problem, please let me know where it is missing the issue.
如果我明白你的需求,这应该可以正常工作。
从中选择*
值 v
if i understand what your needing this should work just fine.
select * from
values v