2013-08-02 99 views
1

看看這些表需要用複雜的一些援助JOIN是SQL查詢

enter image description here

很簡單: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要顯示一個字,如「其他」

+0

使用左連接,將獲取所有匹配的記錄,加左邊的記錄和那些等效的null正確的,在這裏你可以用select替代select ..case – lloydom

回答

2

使用LEFT OUTER當你鏈接VENUE與SOCIETY_TERRITORY和JOIN所以當你與社會的

鏈接SOCIETY_TERRITORY上

注意:當您使用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` 
+0

我不熟悉'left outer join'或者'inner join'這個問題請稍微詳細說明 – Moon

+0

@JunaidSaeed:連接是SQL基礎的一部分,它們不是這裏沒有什麼需要說明的。任何SQL教程都應該涵蓋它們。大量博客文章專門用於加入。以下是一個開始:[SQL加入的可視化解釋](http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html)。 –

0

所有你JOIN s爲INNER JOIN秒。 INNER關鍵字在MySQL中是可選的,並且經常省略(如您的示例中所示)。在需要的地方使用LEFT OUTER JOIN並修改你的SELECT子句以包含諸如「COALESCE(Society_Name,'Other')Society_Name」