编写SQL语句的问题

时间:2011-08-12 09:31:43

标签: mysql sql

我必须写一个包含一个连续包含两个不同值的字段的SQL语句,但是按照我写的方式,它总是返回null,因为它被解释为同时具有两个值!

我的条件应该是:(ci.field ='Group'和ci.oldString ='Triage')和(ci.field ='assignee'和ci.newString不为NULL)

这意味着计算时间:将问题分配给名为Triage的组以及将问题分配给某人时

我该如何解决?

我的SQL声明:

select TIMEDIFF(a.created,b.created) 
from
(select g.created, g.issueid as groupid1 
    from   changegroup g
    join   changeitem ci on (ci.groupid = g.id)
    join   jiraissue ji on (ji.id = g.issueid)
    join   project p on (p.id = ji.project)
    join   priority pr on (pr.id = ji.priority)
    where  ci.field = 'Group'
    and  ci.oldString = 'Triage' 
    and  ci.field='assignee'
    and ci.newString is not NULL
    and  p.pname = 'Test'
    and pr.pname='P1'
    and  ji.created between '2011-08-11 14:01:00' and  '2011-08-12 14:11:00'
) a 
left join (
    select ji.created, ji.id as groupid2 
    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-12 14:11:00' 
) b ON (a.groupid1 = b.groupid2);

这是我应该检索数据的表格

enter image description here

1 个答案:

答案 0 :(得分:0)

请参阅我对您问题质量的评论,但提示如何解决这个问题(假设您可以确保这不会创建1-n联接)

select groupid_orsomething_else, TIMEDIFF(a.created, b.created) 
from yourtable
left join 
 (select groupid_orsomething_else, created 
  from yourtable 
  where field = 'Group' and oldstring is 'Triage'
 ) a 
on a.groupid_orsomething_else = yourtable.groupid_orsomething_else
left join 
 (select groupid_orsomething_else, created 
  from yourtable 
  where field = 'assignee' and oldstring is null) b 
on b.groupid_orsomething_else = yourtable.groupid_orsomething_else