1

我在 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
4

1 回答 1

0

就像 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 的示例。Think 链接在 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
于 2021-02-18T14:08:59.160 回答