0

次のテーブルがあります -Task(id, type, sessionId, termination, scenario) のリストがありsessionIdます。

このセッションに共通するすべてのタスクを選択したいと思います。タスクは、タイプ、終了、およびシナリオが同じであれば同等です。

例 -

| id | type | sessionId | termination | scenario |
  1     A         20        duration      sc1
  2     B         20       invocation     sc1
  3     C         20        duration      sc2
  4     A         21        duration      sc1
  5     B         21       invocation     sc1

list equals (20, 21) の場合sessionId- 次の情報を取得したい

| id | type | sessionId | termination | scenario |
  1     A         20        duration      sc1
  2     B         20       invocation     sc1
  4     A         21        duration      sc1
  5     B         21       invocation     sc1 

ids=1,2,4,5 のタスクは、セッション 20 と 21 で共通です。

私は次のクエリを設計します -

select l.* from Task l 
inner join(select p.* from Task p 
           where 
                p.sessionId in (20,21) 
           group by 
                  p.type, 
                  p.termination, 
                  p.scenario 
           having 
                  count(p.id)=2)s 
on 
   l.type=s.type 
   and l.scenario=s.scenario 
   and l.termination=s.termination;

そのような情報を得る最善の方法はありますか?select1 つの操作のみを含み、より高速に動作する、より優れたクエリがあるのではないでしょうか?

4

2 に答える 2

0

これがアプローチです。すべてのセッションに一致するタイプ、終了、およびシナリオのすべてのペアを検索します。次に、tasksテーブルに再び参加して、すべての情報を取得します。

select t.*
from tasks t join
     (select type, termination, Scenario
      from tasks
      where sessionId in (20, 21)
      group by type, termination, scenario
      having count(distinct sessionId) = 2
     ) tts
     on tts.type = t.type and tts.termiantion = t.termination and tts.scenario = t.scenario
where t.sessionId in (20, 21);

リストと2(リストのサイズを変更する場合) を変更する必要があります。

于 2013-04-01T20:38:55.947 に答える