看看這些表需要用複雜的一些援助JOIN是SQL查詢
很簡單:Venue
包含COUNTRY_ID這是一個FK在Society_Territory
,我們會找到一個society_ID這是一個Society
的FK。我有查詢期間和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_ID
S IN Venue
該不會是在Society_Territory
。我仍然要選擇它們,而不是顯示與實際Society_Name
要顯示一個字,如「其他」
使用左連接,將獲取所有匹配的記錄,加左邊的記錄和那些等效的null正確的,在這裏你可以用select替代select ..case – lloydom