将 SQL Server 图形数据库中的 MATCH 子句和 INNER JOIN 与混合模型相结合
Posted
技术标签:
【中文标题】将 SQL Server 图形数据库中的 MATCH 子句和 INNER JOIN 与混合模型相结合【英文标题】:Combine MATCH clause and INNER JOIN in SQL Server graph database with mixed model 【发布时间】:2020-09-30 05:25:06 【问题描述】:我在 SQL Server 中有一些节点和边缘表,它们与存储用户的标准表具有一对多的关系。每条边和节点表都有这种1N的关系。
我想知道如何使用匹配子句和左连接执行查询:
SELECT * FROM Node1Table n1, EdgeTable e, Node2Table n2
LEFT JOIN UserTable usr ON e.usr = usr.ID
MATCH (n1-(e)->n2)
我可以这样写查询:
SELECT * FROM EdgeTable e
INNER JOIN Node1Table n1 ON e.$from_ID = n1.$node_ID
INNER JOIN Node2Table n2 ON e.$to_ID = n2.$node_ID
LEFT JOIN UserTable usr ON e.usr = usr.ID
但我不知道 n1 是来自还是来自对象。
我无法进行内部连接,因为 e.usr 可能为空
感谢您的帮助
编辑:
测试 1:
SELECT * FROM
OBJ_APPLICATION n1, REL_APPLICATION_RESPONSABLE r INNER JOIN
Management_User u on u.[UserID] = r.[CPQ], OBJ_RESPONSABLE n2
WHERE MATCH(n1-(r)->n2)
error : The identifier "r" in a MATCH clause is used with a JOIN clause or an APPLY operator. JOIN and APPLY are not supported with MATCH clauses.
测试 2:
SELECT * FROM
OBJ_APPLICATION n1, REL_APPLICATION_RESPONSABLE r, OBJ_RESPONSABLE n2
INNER JOIN Management_User u on u.[UserID] = r.[CPQ]
WHERE MATCH(n1-(r)->n2)
error : The multi-part identifier r.CPQ could not be bound
测试 3:
SELECT * FROM
OBJ_APPLICATION n1, REL_APPLICATION_RESPONSABLE r, OBJ_RESPONSABLE n2, Management_User u
WHERE MATCH(n1-(r)->n2)
AND u.[UserID] = r.[CPQ]
Works with an INNER JOIN but in some cases I have to make a LEFT JOIN
【问题讨论】:
我自己从未使用过图形函数,但我建议您先计算图形结果,然后加入用户,例如SELECT * FROM (SELECT * FROM .... MATCH (n1-(e)->n2)) AS g LEFT JOIN UserTable usr ON g.usr = usr.ID
.
谢谢你,但它不起作用
你能说得更具体点吗?你有错误吗?
我的建议返回什么错误?
【参考方案1】:
就像 Alex 建议的那样,您可以进行子查询。以下是我正在积极处理的功能齐全的查询。
SELECT
TokensConnectors.*
FROM
(
SELECT
leftWord.word As LeftSide,
MiddleWord.word AS MiddleWord,
DependsOn1.[DependencyType] As [DependencyType1],
DependsOn1.SentenceId
FROM
Pipeline.Words MiddleWord,
Pipeline.Words leftWord,
Pipeline.DependsOn DependsOn1
WHERE
MATCH(leftWord<-(DependsOn1)-MiddleWord)
AND leftWord.Word = 'differential'
and middleWord.word = 'diagnosis'
) AS DifferentialDiagnosis
INNER JOIN [Pipeline].[DependsOn] AS TokensConnectors ON TokensConnectors.[SentenceId] = DifferentialDiagnosis.SentenceId
这是一个示例,我将 Sentences、ClinicalNotes 和 DependsOn1 进行内部连接。认为链接是在 Where 语句中完成的。
SELECT
DependsOn1.SentenceId
FROM
Pipeline.Words RightWord,
Pipeline.Words LeftWord,
Pipeline.DependsOn DependsOn1,
Pipeline.Sentences,
Pipeline.ClinicalNotes
WHERE
MATCH(LeftWord<-(DependsOn1)-RightWord)
AND LeftWord.Word = 'differential'
and RightWord.word = 'diagnosis'
AND DependsOn1.SentenceId = Sentences.Id
AND ClinicalNotes.Id = Sentences.ClinicalNoteId
AND ClinicalNotes.ProcessingLogId = @processingLogIdessingLogId
【讨论】:
以上是关于将 SQL Server 图形数据库中的 MATCH 子句和 INNER JOIN 与混合模型相结合的主要内容,如果未能解决你的问题,请参考以下文章
实体框架查询 SQL Server 2017 图形数据库的语法
在 Microsoft SQL Server 2017 图形数据库中查询可选关系的语法?