2

我有四个表,我需要从一个具有 where 条件的表中获取数据,并且输出包含来自三个不同表的 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'

4

1 回答 1

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;
于 2013-09-13T15:23:34.370 回答