[英]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
. 很简单:
Venue
包含country_ID ,它是Society_Territory
中的FK,在这里我们会找到一个society_ID ,它是Society
的FK。 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 我在查询过程中有一个Venue_ID,我的目标是获取Society_Name,但有一个转折,但首先让我们获取Society_Name
In the following query only look at JOINS and in there I am gonna add comments with this //
prefix 在下面的查询中,仅查看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`
All is cool till here. 到这里为止一切都很酷。 Now here is the TWIST
现在是TWIST
I will have Country_ID
s in Venue
which will not be in Society_Territory
. 我会
Country_ID
S IN Venue
,不会在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" 而且我仍然想选择它们,而不是显示,而实际的
Society_Name
想要显示诸如“ Other”之类的词
use a LEFT OUTER JOIN when you link VENUE with SOCIETY_TERRITORY and so on when you link SOCIETY_TERRITORY with SOCIETY 当您将VENUE与SOCIETY_TERRITORY链接时使用LEFT OUTER JOIN,等等,当您将SOCIETY_TERRITORY与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. 请注意:当您使用LEFT OUTER JOIN时,所有表都取决于它必须与其他LEFT OUTER JOIN链接,因为如果使用INNER JOIN会取消对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. 您所有的
JOIN
都是INNER JOIN
。 The INNER
keyword is optional in MySQL and frequently omitted (as in your example). INNER
关键字在MySQL中是可选的,并且经常被省略(如您的示例)。 Use a LEFT OUTER JOIN
where required and amend your SELECT clause to include something like "COALESCE(Society_Name,'Other') Society_Name" 在需要的地方使用
LEFT OUTER JOIN
并修改SELECT子句,使其包含诸如“ COALESCE(Society_Name,'Other')Society_Name”的名称
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.