SQL语句的错误结果

时间:2011-08-11 15:27:31

标签: mysql sql

我有一个返回错误结果的请求,我无法修复它,计算错误,

我的查询:

select TIMEDIFF(a.CREATED,b.CREATED) 
from
(
  select changegroup.CREATED 
  from changegroup,changeitem,project,jiraissue
  where  changegroup.id=changeitem.groupid
  and field='Group'
  and oldString='Triage' 
  and jiraissue.id=changegroup.issueid 
  and project.id=jiraissue.project 
  and project.pname='Test'
  and jiraissue.CREATED between '2011-08-11 14:01:00' and  '2011-08-11 14:11:00'  
) a,
(
  select jiraissue.CREATED 
  from jiraissue ,changegroup,project
  where jiraissue.id=changegroup.issueid 
  and project.id=jiraissue.project 
  and project.pname='Test'
  and jiraissue.CREATED between '2011-08-11 14:01:00' and '2011-08-11 14:11:00' 
) b;

子查询1的结果:

enter image description here

子查询2的结果:

enter image description here

查询结果:

enter image description here

结果需要:

enter image description here

表之间的关系

enter image description here

谢谢

1 个答案:

答案 0 :(得分:0)

首先,我会按如下方式重写您的查询,只是为了清楚地显示查询中表格之间的联系:

select TIMEDIFF(a.created,b.created) 
from
    (
        select g.created
        from   changegroup g
        join   changeitem ci on (ci.groupId = g.id)
        join   jiraissue ji on (ji.id = g.id)
        join   project p on (p.id = ji.project)
        where  ci.field = 'Group'
               and ci.oldString = 'Triage' 
               and p.pname = 'Test'
               and ji.created between '2011-08-11 14:01:00' and  '2011-08-11 14:11:00'
    ) a
join    (
        select ji.created
        from   jiraissue ji
        join   changegroup g on (g.issueid = ji.id)
        join   project p on (p.id = ji.project)
        where  p.pname = 'Test'
          and  ji.created between '2011-08-11 14:01:00' and '2011-08-11 14:11:00' 
    ) b;

我认为你错过了两个子查询之间的链接。这实际上取决于你想要完成的事情。如果我理解你很好,并且你只是想计算jira问题创建日期和问题所属的创建日期之间的时差,那么下面的查询应该可以解决问题(更多的是,基于你的查询):

select TIMEDIFF(a.created,b.created) 
from
    (
        select g.created, g.id as groupid
        from   changegroup g
        join   changeitem ci on (ci.groupId = g.id)
        join   jiraissue ji on (ji.id = g.id)
        join   project p on (p.id = ji.project)
        where  ci.field = 'Group'
      and  ci.oldString = 'Triage' 
      and  p.pname = 'Test'
      and  ji.created between '2011-08-11 14:01:00' and  '2011-08-11 14:11:00'
    ) a
left join   (
        select ji.created, g.id as groupid
        from   jiraissue ji
        join   changegroup g on (g.issueid = ji.id)
        join   project p on (p.id = ji.project)
        where  p.pname = 'Test'
          and  ji.created between '2011-08-11 14:01:00' and '2011-08-11 14:11:00' 
    ) b ON (a.groupid = b.groupid)

请详细说明你想要完成什么,也许我可以提供更多帮助。