我知道連接的用法,但有時遇到這樣的情況時,我無法確定哪個加入合適,左或右。如何決定何時使用正確的連接/左連接或內連接或者如何確定哪個表位於哪一側?
這裏是我卡在哪裏的查詢。
SELECT count(ImageId) as [IndividualRemaining],
userMaster.empName AS ID#,
CONVERT(DATETIME, folderDetails.folderName, 101) AS FolderDate,
batchDetails.batchName AS Batch#,
[email protected],
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 [email protected] 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
右連接是除與所述表的順序一樣左連接切換。 FROM TableA LEFT JOIN TableB == FROM TableB RIGHT JOIN TableA(儘管如果您使用SELECT *,列的順序將會不同,因爲它按照表順序發出列) – GalacticCowboy 2010-07-22 17:08:47
可能的重複[INNER JOIN, LEFT JOIN,RIGHT JOIN,FULL JOIN)in Mysql](http://stackoverflow.com/questions/5706437/whats-the-difference-between-inner-join-left-join-right-join-full-join-in -mys) – JonH 2012-02-09 16:33:20
@JonH:查看兩者的發佈日期。如果它的愚蠢,我肯定有時間機器或我overflooded StackOverflow。 – 2012-02-10 09:58:01