Transact-SQL 查询/如何组合多个 JOIN 语句?

发布于 2024-09-15 19:09:15 字数 1561 浏览 3 评论 0原文

几个小时以来我一直在思考这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好倦 2024-09-22 19:09:15

我觉得你的问题中我遗漏了一些东西。如果此解决方案不能解决问题,请告诉我哪里缺少问题。

SELECT
    Values.Value
FROM
    Values INNER JOIN Fields ON
        Values.FieldId = Fields.FieldId  
    INNER JOIN FormFields ON
        Fields.FieldId = FormFields.FieldId  
    INNER JOIN Forms ON
        FormFields.FormId = Forms.FormId
    INNER JOIN DataPools ON
        Forms.FormId = DataPools.FormId
WHERE
    Fields.FieldName = @FieldName
    AND
    Forms.FormName = @FormName
    AND
    DataPools.PoolName = @PoolName;

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.

SELECT
    Values.Value
FROM
    Values INNER JOIN Fields ON
        Values.FieldId = Fields.FieldId  
    INNER JOIN FormFields ON
        Fields.FieldId = FormFields.FieldId  
    INNER JOIN Forms ON
        FormFields.FormId = Forms.FormId
    INNER JOIN DataPools ON
        Forms.FormId = DataPools.FormId
WHERE
    Fields.FieldName = @FieldName
    AND
    Forms.FormName = @FormName
    AND
    DataPools.PoolName = @PoolName;
独行侠 2024-09-22 19:09:15

如果我明白你的需求,这应该可以正常工作。

从中选择*
值 v

join records r
on v.recordid = r.recordid
join datapool dp 
on r.poolid = dp.poolid
join forms f
on r.formid = f.formid
join fields fi
on v.fieldid = fi.fieldid

where
    fi.FieldName = @FieldName
    AND
    f.FormName = @FormName
    AND
    dp.PoolName = @PoolName;

if i understand what your needing this should work just fine.

select * from
values v

join records r
on v.recordid = r.recordid
join datapool dp 
on r.poolid = dp.poolid
join forms f
on r.formid = f.formid
join fields fi
on v.fieldid = fi.fieldid

where
    fi.FieldName = @FieldName
    AND
    f.FormName = @FormName
    AND
    dp.PoolName = @PoolName;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文