一尘不染

如何确定何时使用右连接/左连接或内部连接,或者如何确定哪张表在哪一边?

mysql

我知道加入的使用情况,但有时我遇到这样的情况时,我不能决定哪些 加入 将是合适的,一个 向左或向右

这是我卡住的查询。

    SELECT  count(ImageId) as [IndividualRemaining],
                userMaster.empName AS ID#,
                CONVERT(DATETIME, folderDetails.folderName, 101) AS FolderDate,
                batchDetails.batchName AS Batch#,
                Client=@ClientName,
                TotalInloaded = IsNull(@TotalInloaded,0),
                PendingUnassigned = @PendingUnassigned,
                InloadedAssigned =     IsNull(@TotalAssigned,0),
                TotalProcessed = @TotalProcessed,
                Remaining = @Remaining
        FROM
                batchDetails
                    Left JOIN  folderDetails ON batchDetails.folderId = folderDetails.folderId
                    Left JOIN  imageDetails ON batchDetails.batchId = imageDetails.batchId
                    Left JOIN  userMaster ON imageDetails.assignedToUser = userMaster.userId

        WHERE   folderDetails.ClientId =@ClientID and verifyflag='n'
                and folderDetails.FolderName IN (SELECT convert(VARCHAR,Value) FROM dbo.Split(@Output,','))
                and userMaster.empName <> 'unused'

        GROUP BY userMaster.empName, folderDetails.folderName, batchDetails.batchName

        Order BY folderDetails.Foldername asc

阅读 522

收藏
2020-05-17

共1个答案

一尘不染

是的,这取决于您所处的情况。

为什么要使用SQL JOIN?

:每当必须通过SQL SELECT语句访问多个表时,都应使用SQL JOIN;如果JOINed表之间不匹配,则不应该返回任何结果。

阅读有关代码项目的原始文章将对您有很大帮助:SQL
Joins的可视表示

替代文字

还请检查此帖子:SQL SERVER
–更好的性能–左联接还是不联接?

在以下位置找到原始版本:MySQL中的JOIN和OUTER
JOIN之间的区别

2020-05-17