0

我一直在尝试获取状态,因此我可以找出错误更改其状态“已解决”需要多长时间,因此我有以下 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,但不使用 have 子句,我会得到以下结果:

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

任何帮助表示赞赏。

4

2 回答 2

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)
于 2013-04-09T13:31:20.150 回答
0

几件事。首先,我认为默认情况下您不能在 HAVING 子句中使用别名。我可能是错的,我无法在这里解析文档。所以 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'      

试试看。

于 2013-04-09T13:43:41.967 回答