2017-01-12 22 views
1

Mantis Bug Database(MySQLを使用)を使用して、過去2週間以内に重大度に変化があったバグをクエリしたいバグを示す必要があります。MySQL:タイムスタンプ上でmax関数を使用した望ましくない結果

問題は、bugIDごとに複数のエントリ(プライマリキー)が表示されることです。これは、バグごとに最新の変更のみが必要なので、私の望む結果ではありません。これは、どういうわけか、max関数とgroup by節を不正に使用していることを意味します。私はこのクエリで3つのエントリを取得する例えばID 8とバグについて

SELECT `bug_id`, 
    max(date_format(from_unixtime(`mantis_bug_history_table`.`date_modified`),'%Y-%m-%d %h:%i:%s')) AS `Severity_changed`, 
    `mantis_bug_history_table`.`old_value`, 
    `mantis_bug_history_table`.`new_value` 
    from `prepared_bug_list` 
    join `mantis_bug_history_table` on `prepared_bug_list`.`bug_id` = `mantis_bug_history_table`.`bug_id` 
    where (`mantis_bug_history_table`.`field_name` like 'severity') 
    group by `bug_id`,`old_value`,`.`new_value` 
    having (`Severity_modified` >= (now() - interval 2 week)) 
    order by bug_id` ASC 

ここでは、私のクエリを見ることができます。 id 8のバグは、実際には過去2週間で3つの重大度の変更がありましたが、最新の重大度の変更を取得したいだけです。

質問にはどのような問題がありますか?

答えて

1

を有していてもよく、私は最終的に解決策を持っています!私の友人が私を助けてくれました。解決の1つの部分は、bug_idではなく、連続番号である列IDであるmantisバグ履歴テーブルのプライマリキーを含めることでした。しかし、私が参加しましたサブクエリ(getLatest)が役立つかを理解していない、あなたの迅速な回答を

select `prepared_bug_list`.`bug_id` AS `bug_id`, 
`mantis_bug_history_table`.`old_value` AS `old_value`, 
`mantis_bug_history_table`.`new_value` AS `new_value`, 
`mantis_bug_history_table`.`type` AS `type`, 
date_format(from_unixtime(`mantis_bug_history_table`.`date_modified`),'%Y-%m-%d %H:%i:%s') AS `date_modified` 
FROM `prepared_bug_list` 
JOIN mantis_import.mantis_bug_history_table 
ON `prepared_bug_list`.`bug_id` = mantis_bug_history_table.bug_id 
where (mantis_bug_history_table.id = -- id = that is the id of every history entry, not confuse with bug_id 
    (select `mantis_bug_history_table`.`id` from `mantis_bug_history_table` 
    where ((`mantis_bug_history_table`.`field_name` = 'severity') 
    and (`mantis_bug_history_table`.`bug_id` = `prepared_bug_list`.`bug_id`)) 
    order by `mantis_bug_history_table`.`date_modified` desc limit 1) 
and `date_modified` > unix_timestamp() - 14*24*3600) 
order by `prepared_bug_list`.`bug_id`,`mantis_bug_history_table`.`date_modified` desc 
1

max()は集計関数であり、実行しようとしているものには適していません。

あなたがしようとしていることは、適用可能なすべてのbug_idのうち最新のものをmantis_bug_history_tableに入れることです。それが本当であるならば、私は次のようにクエリを書き直すでしょう - 私は、サブクエリgetLatestを書いて、prepared_bug_list

更新答え

注意して、それに参加するだろう:私が持っていないアクセス実際のDBテーブルにので、このクエリはバグ

select 
    `getLatest`.`last_bug_id` 
    , `mantis_bug_history_table`.`date_modified` 
    , `mantis_bug_history_table`.`old_value` 
    , `mantis_bug_history_table`.`new_value` 
from 
    (
     select 
       (
       select 
        `bug_id` 
       from 
        `mantis_bug_history_table` 
       where 
        `date_modified` > unix_timestamp() - 14*24*3600 -- two weeks 
        and `field_name` like 'severity' 
        and `bug_id` = `prepared_bug_list`.`bug_id` 
       order by 
        `date_modified` desc 
       limit 1 
      ) as `last_bug_id` 
     from 
      `prepared_bug_list` 
    ) as `getLatest` 
    inner join `mantis_bug_history_table` 
      on `prepared_bug_list`.`bug_id` = `getLatest`.`last_bug_id`  
order by `getLatest`.`bug_id` ASC 
+0

ありがとう: ソリューションの別の部分は、where句のサブクエリました。制限1では、クエリ全体に対して1行しか得られないことが示されています(結果は1つのバグです)。私はサブクエリであなたの意図は、バグごとに最新の結果を取得することでした(降順で日付をソートする(最新のものを上に置く)とそれはバグ1つの結果で制限しますが、意図したとおりには動作しません。 –

+1

@Bruder - あなたのDBにアクセスすることはできませんので、私はあなたの目で見ているだけではなく、私のクエリをデバッグすることはできません。サブクエリを取り出して何が得られるかを見るだけでトラブルシューティングできます。おそらくあなたのデータには過去2週間に1つのバグしかありませんでしたか?がんばろう! (または、データとテーブルをSqlFiddle.comに置くことができ、さらにクエリをデバッグできます。) – leeyuiwah

+0

最後の2週間に複数のバグのエントリが複数あります。 しかし、これらのすべてのバグについては、各バグの最新のエントリを取得したかったのです。 あなたが提案したサブクエリは私の場合は機能しません。 –

関連する問題