我正在尝试构建一个查询来输出 Comments
、Comment_Date
、Username
或 First_Name
,具体取决于是否UserID
或 StaffID
出现在表格行中。
我可以仅使用 UserID
或 StaffID
来处理它,但是当我添加这两个联接时,它不会显示任何内容。
所以我再次需要输出 Comments
、Comment_Date
、Username
和 First_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');
最佳答案
Report_Comments
加入登录
员工
ON Report_Comments
。用户ID
=
登录
。LoginID、Report_Comments
。UserID_Staff
上面缺少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'
关于mysql - 查询以显示两个连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36922893/