简体   繁体   中英

Need some assistance with a complex JOIN is SQL Query

Take a look at these tables

在此处输入图片说明

It's simple: Venue contains country_ID which is an FK in Society_Territory where we will find a society_ID which is an FK of Society . I have a Venue_ID during the query and my objective is to get the Society_Name but there is a twist but first lets just get the Society_Name

In the following query only look at JOINS and in there I am gonna add comments with this // prefix

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`

All is cool till here. Now here is the TWIST

I will have Country_ID s in Venue which will not be in Society_Territory . And I still want to select them and instead of showing and actual Society_Name want to show a word such as "Other"

use a LEFT OUTER JOIN when you link VENUE with SOCIETY_TERRITORY and so on when you link SOCIETY_TERRITORY with SOCIETY

Pay attention: When you use a LEFT OUTER JOIN all tables depends by its must be linked with other LEFT OUTER JOIN because if you use INNER JOIN you cancel di effects on LEFT.

Edit:

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`

All your JOIN s are INNER JOIN s. The INNER keyword is optional in MySQL and frequently omitted (as in your example). Use a LEFT OUTER JOIN where required and amend your SELECT clause to include something like "COALESCE(Society_Name,'Other') Society_Name"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM