内部连接来自3个表

时间:2014-08-20 16:48:56

标签: sql join inner-join

尝试从3个表格中显示时,内部连接有困难。它们的结构如下图所示:

http://pbrd.co/1odLBZy

我想要实现的是从任务表中选择以下内容:

  • ProjectID(as project.Name)
  • WorkerID(作为user.Username)
  • 名称
  • OrderInProject
  • TimeSpent
  • 描述
  • DUEDATE

如何构建SQL查询?

3 个答案:

答案 0 :(得分:3)

SELECT p.Name AS ProjectID, u.Username AS WorkerID, t.Name, t.OrderInProject,
       t.TimeSpent, t.Description, t.DueDate
FROM task t
INNER JOIN user u ON t.WorkerID = u.UserID
INNER JOIN project p ON t.ProjectID = p.ProjectID

如果您还想获得Username的{​​{1}},请使用以下内容:

ProjectManagerID

答案 1 :(得分:0)

为了回答这样的事情,最好从单个表开始,然后添加列和其他表来测试每个添加项。那说以下应该是关闭的:

SELECT t.Name,
       t.OrderInProject,
       t.TimeSpent,
       t.Description,
       t.DueDate,
       p.Name,
       u.Username
  FROM task t
       INNER JOIN project p ON p.ProjectID = t.ProjectID
       INNER JOIN user u ON u.UserId = t.WorkerID

祝你好运!

答案 2 :(得分:0)

SELECT t.Name
    , t.OrderInProject
    , t.TimeSpent
    , t.Description
    , t.DueDate
    , u.Username
    , p.Name
FROM task t
INNER JOIN project p
    on t.ProjectId = p.ProjectId
INNER JOIN user u
    on t.WorkerId = u.UserId