获取已解决的错误的最后结果

时间:2013-04-09 13:11:53

标签: mysql sql mantis

我一直试图获得一个状态,所以我可以找出已经花了多长时间来更改其状态'已解决',所以我有以下SQL:

select bug.id,
       bug.project_id,
       DATEDIFF (FROM_UNIXTIME(his.date_modified), FROM_UNIXTIME(date_submitted)) as dias_resolucao,
       DATE_FORMAT(FROM_UNIXTIME(his.date_modified), '%Y-%m') as ano_mes
from mantis_bug_table bug
left join mantis_project_table pro on bug.project_id = pro.id
left join mantis_custom_field_string_table cus on bug.id = cus.bug_id
left join mantis_bug_history_table his on bug.id = his.bug_id
where bug.category_id = 1 and
      (cus.field_id=1 or cus.field_id is null) and      
      his.new_value = 80 and 
      his.field_name = 'status' and                 
      bug.id = 5171 and
      cus.value='Sim'      
having his.date_modified = max(his.date_modified)

问题是我不知道为什么会出现以下错误:

[SELECT - 0 row(s), 0.000 secs]  
[Error Code: 1054, SQL State: 42S22]  
Unknown column 'his.date_modified' in 'having clause'

运行相同的SQL,没有having子句会带来以下结果:

╔══════╦════════════╦════════════════╦═════════╗
║  id  ║ project_id ║ dias_resolucao ║ ano_mes ║
╠══════╬════════════╬════════════════╬═════════╣
║ 5171 ║          4 ║             17 ║ 2012-12 ║
║ 5171 ║          4 ║             18 ║ 2012-12 ║
╚══════╩════════════╩════════════════╩═════════╝

感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

试试吧。

SELECT his.date_modified, bug.id,
       bug.project_id,
       DATEDIFF (FROM_UNIXTIME(his.date_modified), FROM_UNIXTIME(date_submitted)) AS dias_resolucao,
       DATE_FORMAT(FROM_UNIXTIME(his.date_modified), '%Y-%m') AS ano_mes
FROM mantis_bug_table bug
LEFT JOIN mantis_project_table pro ON bug.project_id = pro.id
LEFT JOIN mantis_custom_field_string_table cus ON bug.id = cus.bug_id
LEFT JOIN mantis_bug_history_table his ON bug.id = his.bug_id
WHERE bug.category_id = 1 AND
      (cus.field_id=1 OR cus.field_id IS NULL) AND      
      his.new_value = 80 AND 
      his.field_name = 'status' AND                 
      bug.id = 5171 AND
      cus.value='Sim'      
HAVING his.date_modified = MAX(his.date_modified)

答案 1 :(得分:0)

一些事情。首先,我认为默认情况下你不能在HAVING子句中使用别名。我可能错了,我在解析文档here时遇到了问题。所以HAVING行应该更像是“HAVING mantis_bug_history_table.date_modified = MAX(mantis_bug_history_table.date_modified)

我可能错了。也就是说,我认为您需要进行自我加入才能获得MAX date_modified。像这样:

select bug.id,
       bug.project_id,
       DATEDIFF (FROM_UNIXTIME(his.date_modified), FROM_UNIXTIME(date_submitted)) as dias_resolucao,
       DATE_FORMAT(FROM_UNIXTIME(his.date_modified), '%Y-%m') as ano_mes
from mantis_bug_table bug
left join mantis_project_table pro on bug.project_id = pro.id
left join mantis_custom_field_string_table cus on bug.id = cus.bug_id
left join mantis_bug_history_table his on bug.id = his.bug_id
inner join (select bug_id, max(date_modified) as max_date_modified from mantis_bug_history_table group by bug_id) as his_max_dates
 on his.bug_id = his_max_dates.bug_id and his.date_modified = his_max_dates.max_date_modified
where bug.category_id = 1 and
      (cus.field_id=1 or cus.field_id is null) and      
      his.new_value = 80 and 
      his.field_name = 'status' and                 
      bug.id = 5171 and
      cus.value='Sim'      

试一试。