2016-04-28 49 views
0

我想建立一個查詢,以輸出CommentsComment_DateUsernameFirst_Name取決於是否UserIDStaffID錶行中pressent。查詢,以顯示兩個連接

我可以只用UserIDStaffID工作,但是當我添加這兩個連接時,它什麼都不顯示。

因此,我需要再次輸出Comments,Comment_Date,UsernameFirst_Name。 任何幫助表示讚賞。

我查詢

select('Report_Comments.Comments, Report_Comments.Comment_Date, Login.Username, staff.First_Name') 
     ->from('Report_Comments') 
     ->join('Login staff', 'Report_Comments.UserID = Login.LoginID') 
     ->join('staff', 'Report_Comments.UserID_Staff = staff.StaffID'); 

回答

0

你可以從documentation找出(搜索「加盟」),調用join()有兩個必需的參數。第一個是加入的,第二個是加入條件。顯然你將兩個表和兩個連接條件壓縮到它的參數中,這就是CodeIgniter生成的查詢有錯誤的原因。

您的代碼應閱讀:

select('Report_Comments.Comments, Report_Comments.Comment_Date, Login.Username, 
    staff.First_Name') 
->from('Report_Comments') 
->join('Login', 'Report_Comments.UserID = Login.LoginID') 
->join('staff', 'Report_Comments.UserID_Staff = staff.StaffID') 
->where('reportID', '53'); 
+0

嗯,它不通過這種方式得到的數據,如果我只有一個連接它的工作,但與兩個它不會檢索數據。但它似乎是正確的。我有一個戲。 – Beep

+0

它可以工作,如果我刪除了加入,但他們不一起工作 – Beep

1

Report_Comments JOIN Loginstaff ON Report_CommentsUserID =
LoginLoginID, Report_CommentsUserID_Staff

上面缺少join關鍵字。如果你想要join表,他們都必須是join與明確的條件。

更正它:

SELECT `Report_Comments`.`Comments`, `Report_Comments`.`Comment_Date`, `Login`.`Username`, `staff`.`First_Name` 
FROM `Report_Comments` 
JOIN `Login` ON `Report_Comments`.`UserID` = `Login`.`LoginID 
JOIN `staff` ON `Report_Comments`.`UserID_Staff = `staff`.`StaffID` 
WHERE `ReportID` = '53' 
+0

尼斯,快速回復表示感謝。 il閱讀此內容,嘗試一下,並讓你知道back ticks的+1。 – Beep

+0

加入條件中的反引號不匹配。 – axiac

+1

@ axiac..just看到thatcororcted它。 –