我有四个表,我需要从一个表中获取数据,其中condition和输出包含来自三个不同表的ID,使用这些ID来获取它们的名称。
Company:
CompanyID - PK
CompanyName
CompanyDescription
Users:
UserID - PK
FirstName
LastName
Email
TaskDetails:
TaskID - PK
CompanyID - FK of Company.CompanyID
TaskStatus:
TaskStatusID - PK
TaskID - FK of TaskDetails.TaskID
Status
Details
CreatedBy - FK of Users.UserID
UpdatedBy - FK of Users.UserID
CreatedAt
UpdatedAt
需要查询返回如下内容:
CompanyName,Email,Status,Details,CreatedAt,UpdatedAt with where condition on TaskStatus table TaskStatus.UpdatedBy!=1 and TaskStatus.UpdatedAt>'2013-08-01' and TaskStatus.status='COMPLETED'
答案 0 :(得分:1)
Select CM.CompanyName as CompanyName, U.Email as User, TS.Status as Status, TS.Comments as Comments, TS.CreateDate as CreateDate, TS.UpdateDate as UpdateDate
FROM
TaskStatus as TS
LEFT JOIN TaskDetails as TD ON TD.TaskID = TS.TaskID
LEFT JOIN Company as CM on CM.CloudID = TD.CloudID
LEFT JOIN Users as U on U.UserID = TS.UpdatedBy
WHERE
TS.CreateDate>'2013-08-01' and TS.UpdatedBy!=1 and TS.Status='COMPLETED'
GROUP by CM.CompanyName, U.Email;