2

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

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

基本上,我想根据tr_type检索project_nametask_name ,它可以是“项目”或“任务”的值

一个例子

时间跟踪

+----+------------+--------+---------+
| 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 |
+----+------------------------+

时间任务

+----+---------------------+
| 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

如果有人对如何做到这一点有任何想法,请随时分享!


更新:看起来我忘了指定我正在使用access database。这显然不接受像CASEor coalesce.. 之类的东西。显然有,IIF()但我不太确定在这种情况下如何使用它。

4

4 回答 4

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子句可能包含任何条件,甚至是不是来自连接表的条件(如本例所示)。许多程序员假定条件必须只是那些与正式外键关系有关的条件。

于 2012-11-22T19:02:08.747 回答
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
于 2012-11-22T19:15:51.550 回答
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'

这将为您提供所需的确切表格结果

于 2012-11-22T19:05:00.440 回答
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用来挑选出成功的连接值。

于 2012-11-22T19:07:11.810 回答