1

看看这些表

在此处输入图像描述

很简单:Venue包含country_ID这是一个 FK Society_Territory,我们将在其中找到一个Society_ID,它是一个 FK Society。我在查询期间有一个 Venue_ID,我的目标是获取Society_Name但有一个转折,但首先让我们获取Society_Name

在下面的查询中,只查看JOINS,我将在其中添加带有此//前缀的注释

SELECT
    uuid()AS `UUID`,
    `pc`.`PRSClaimID` AS `prsclaimid`,      
    `a`.`LoginName` AS `loginname`,
    `a`.`BandName` AS `bandname`,
    `smartistdetails`.`LoginName` AS `createdbyloginname`,          
    `Society`.`Society_Name` AS societyName
    count(
        `smliveclaims`.`LiveclaimsID`
    )AS `gigcount`    

FROM `smprsliveclaimlink`

JOIN `smliveclaims` ON `smprsliveclaimlink`.`fkLiveClaimID` = `smliveclaims`.`LiveclaimsID`

// Here I have the Venue_ID from smliveclaims so i starting moving towards society name

JOIN Venue ON `smliveclaims`.fk_venueId = Venue.Venue_ID
JOIN Society_Territory ON Venue.Country_ID = Society_Territory.Country_ID
JOIN Society ON Society_Territory.Society_Id = Society.Society_ID    

// Now from Society i can select the Society_Name which i am already doing in the query above

JOIN `smartistdetails` `a`
JOIN `smprsclaims` `pc` ON `a`.`ArtistID` = `pc`.`fkArtistID`
JOIN `smcategories` ON `pc`.`FK_CategoryID` = `smcategories`.`Id`
JOIN `smcategoriestype` ON  `smcategories`.`fk_CategoryTypeId` = `smcategoriestype`.`Id`
JOIN `smartistdetails` ON `pc`.`CreatedBy` = `smartistdetails`.`ArtistID` AND `smprsliveclaimlink`.`fkPRSClaimID` = `pc`.`PRSClaimID`

GROUP BY
    `a`.`LoginName`,
    `a`.`BandName`,
    `smcategories`.`Id`,
    `smcategoriestype`.`CategoryType`,
    `smartistdetails`.`LoginName`

到这里为止一切都很酷。现在这里是TWIST

我将有Country_IDs 在Venue其中不会在Society_Territory. 而且我仍然想选择它们,而不是显示和实际Society_Name想要显示诸如“其他”之类的词

4

2 回答 2

2

当您将 VENUE 与 SOCIETY_TERRITORY 链接时使用 LEFT OUTER JOIN 等等当您将 SOCIETY_TERRITORY 与 SOCIETY 链接时

注意:当您使用 LEFT OUTER JOIN 时,所有表都依赖于它必须与其他 LEFT OUTER JOIN 链接,因为如果您使用 INNER JOIN,您会取消对 LEFT 的影响。

编辑:

SELECT
    uuid()AS `UUID`,
    `pc`.`PRSClaimID` AS `prsclaimid`,      
    `a`.`LoginName` AS `loginname`,
    `a`.`BandName` AS `bandname`,
    `smartistdetails`.`LoginName` AS `createdbyloginname`,          
    coalesce(`Society`.`Society_Name`, 'Other') AS societyName
    count(`smliveclaims`.`LiveclaimsID`)AS `gigcount`    
FROM `smprsliveclaimlink`
JOIN `smliveclaims`
    ON `smprsliveclaimlink`.`fkLiveClaimID` = `smliveclaims`.`LiveclaimsID`
// Here I have the Venue_ID from smliveclaims so i starting moving towards society name
JOIN Venue ON `smliveclaims`.fk_venueId = Venue.Venue_ID
LEFT OUTER JOIN Society_Territory ON Venue.Country_ID = Society_Territory.Country_ID
LEFT OUTER JOIN Society ON Society_Territory.Society_Id = Society.Society_ID    
// Now from Society i can select the Society_Name which i am already doing in the query above
JOIN `smartistdetails` `a`
JOIN `smprsclaims` `pc` ON `a`.`ArtistID` = `pc`.`fkArtistID`
JOIN `smcategories` ON `pc`.`FK_CategoryID` = `smcategories`.`Id`
JOIN `smcategoriestype` ON  `smcategories`.`fk_CategoryTypeId` = `smcategoriestype`.`Id`
JOIN `smartistdetails` ON `pc`.`CreatedBy` = `smartistdetails`.`ArtistID` AND `smprsliveclaimlink`.`fkPRSClaimID` = `pc`.`PRSClaimID`
GROUP BY
    `a`.`LoginName`,
    `a`.`BandName`,
    `smcategories`.`Id`,
    `smcategoriestype`.`CategoryType`,
    `smartistdetails`.`LoginName`
于 2013-08-02T09:33:38.900 回答
0

你所有的JOINs都是INNER JOINs。该INNER关键字在 MySQL 中是可选的,并且经常被省略(如您的示例中所示)。在需要的LEFT OUTER JOIN地方使用并修改您的 SELECT 子句以包含类似“COALESCE(Society_Name,'Other') Society_Name”的内容

于 2013-08-02T09:57:31.167 回答