SQL连接3个表(基于2个标准?)

时间:2012-11-22 18:59:06

标签: sql ms-access join

我有3个这样的表设置(稍微简化):

time_tracking:id,tr_proj_id,tr_min,tr_type
time_projects:id,project_name
time_tasks:id,task_name

基本上,我想根据 tr_type 检索 project_name task_name ,它可以是“project”或“task”< / p>

一个例子

time_tracking

+----+------------+--------+---------+
| id | tr_proj_id | tr_min | tr_type |
+----+------------+--------+---------+
|  1 |          3 |     60 | project |
|  2 |          3 |    360 | task    |
|  3 |          1 |    120 | project |
|  4 |          2 |     30 | project |
|  5 |          2 |     30 | task    |
|  6 |          1 |     90 | task    |
+----+------------+--------+---------+

time_projects

+----+------------------------+
| id |      project_name      |
+----+------------------------+
|  1 | Make someone happy     |
|  2 | Start a project        |
|  3 | Jump out of the window |
+----+------------------------+

time_tasks

+----+---------------------+
| id |      task_name      |
+----+---------------------+
|  1 | drink a beer        |
|  2 | drink a second beer |
|  3 | drink more          |
+----+---------------------+

所需的输出

+----+------------------------+------------+--------+---------+
| id |          name          | tr_proj_id | tr_min | tr_type |
+----+------------------------+------------+--------+---------+
|  1 | Jump out of the window |          3 |     60 | project |
|  2 | drink more             |          3 |    360 | task    |
|  3 | Make someone happy     |          1 |    120 | project |
|  4 | Start a project        |          2 |     30 | project |
|  5 | drink a second beer    |          2 |     30 | task    |
|  6 | drink a beer           |          1 |     90 | task    |
+----+------------------------+------------+--------+---------+

在整个JOIN事情中真的很糟糕,到目前为止,这是我唯一能做到的事情(这不起作用..):

SELECT tt.tr_proj_id, tt.tr_type, tt.tr_min, pp.project_name, pp.id, ta.task_name, ta.id
FROM time_tracking as tt, time_projects as pp, time_tasks as ta 
WHERE ((tt.tr_type = 'project' AND pp.id = tt.tr_proj_id) OR (tt.tr_type = 'task' AND ta.id = tt.tr_proj_id)) 
AND tt.tr_min > 0
ORDER BY tt.tr_proj_id DESC

如果有人知道如何做到这一点,请随时分享!


更新:看起来我忘了指定我使用访问数据库。这显然不接受CASEcoalesce之类的内容。显然有IIF()但我不太确定在这种情况下如何使用它。

4 个答案:

答案 0 :(得分:2)

使用join子句并将您的连接条件从where子句移动到on子句中:

SELECT
    tt.tr_proj_id,
    tt.tr_type,
    tt.tr_min,
    pp.project_name,
    pp.id,
    ta.task_name,
    ta.id
FROM time_tracking as tt
left join time_projects as pp on tt.tr_type = 'project' AND pp.id = tt.tr_proj_id
left join time_tasks as ta on tt.tr_type = 'task' AND ta.id = tt.tr_proj_id
WHERE tt.tr_min > 0
ORDER BY tt.tr_proj_id DESC,tt.tr_day ASC

我使用了left join,即使连接不存在,也会从主表中获取一行(如果没有连接,则从连接表中的列获取空值)


许多SQL程序员没有意识到的一个关键点是ON子句可能包含任何条件,甚至不包括连接表中的条件(如本例所示)。许多程序员认为条件必须只是那些与正式外键关系有关的条件。

答案 1 :(得分:0)

在两个联接上执行联合:

select tt.id, tp.project_name name, tt.tr_proj_id, tt.tr_min, tt.tr_type
  from time_tracking tt
       inner join time_projects tp on tp.id = tt.tr_proj_id
 where tt.tr_type = 'project'
union all
select tt.id, tp.project_name name, tt.tr_proj_id, tt.tr_min, tt.tr_type
  from time_tracking tt
       inner join time_tasks tk on tk.id = tt.tr_proj_id
 where tt.tr_type = 'task'

这将为您提供您想要的确切表格结果

答案 2 :(得分:0)

SELECT
    time_tracking.id,
    time_tracking.tr_min,
    time_tracking.tr_type,
    coalesce(time_projects.project_name, time_tasks.task_name) as name
FROM time_tracking
LEFT OUTER JOIN time_projects on time_projects.id = time_tracking.tr_proj_id AND time_tracking.tr_type = 'project'
LEFT OUTER JOIN time_tasks on time_tasks.id = time_tracking.tr_proj_id AND time_tracking.tr_type = 'task'
WHERE time_tracking.tr_min > 0
ORDER BY time_tracking.id DESC -- ...

coalesce是MSSQL,在其他数据库技术中有等价的ISNULL

您的想法是加入表格,如果加入失败,您将获得加入失败的NULL。然后使用COALESCE选择成功的连接值。

答案 3 :(得分:0)

试试这个:

SELECT
    tt.id,
    CASE WHEN tt.tr_type = 'project' THEN pp.project_name
         WHEN tt.tr_type = 'task' THEN ta.task_name END as name,
    tt.tr_proj_id,
    tt.tr_type,
    tt.tr_min,
FROM time_tracking as tt
   left join time_projects as pp on pp.id = tt.tr_proj_id
   left join time_tasks as ta on ta.id = tt.tr_proj_id
WHERE tt.tr_min > 0
ORDER BY tt.tr_proj_id DESC