teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

質問編集

2017/12/13 08:00

投稿

aizawal-hiroki
aizawal-hiroki

スコア56

title CHANGED
File without changes
body CHANGED
@@ -1,19 +1,23 @@
1
1
  やりたいことはtesttable1,testtable2という二つのテーブルを結合してDBから最新のエラーを取得したいというものです。
2
2
 
3
- SELECT name,code,testid,message,time FROM testdb.testtable1
3
+ SELECT t1.name,t1.code,t2.testid,t2.message,t2.time FROM testdb.testtable1 t1
4
- INNER JOIN testdb.testtable2
4
+ INNER JOIN testdb.testtable2 t2
5
- ON testtable1.id = testtable2.testid
5
+ ON t1.id = t2.testid
6
6
  and message like '%error%'
7
7
  ;
8
8
 
9
+ [テーブルの中に入っているもの]
10
+ testtable1 : name,code
11
+ testtable2 : testid,name,time
12
+
9
13
  【わからないこと】
10
14
  上のsql文でtesttable1のidとtesttable2のtestidを結びつけました。そのうえでmessageの中にあるerrorというものをあいまい検索しているのですが、ここからtimeで取得した日時から一番新しいものだけを取得したいと思っているのですがどのようにすればいいかわかりません。
11
15
 
12
- SELECT name,code,testid,message,time FROM testdb.testtable1
16
+ SELECT t1.name,t1.code,t2.testid,t2.message,t2.time FROM testdb.testtable1 t1
13
17
  INNER JOIN testdb.testtable2
14
- ON testtable1.id = testtable2.testid
18
+ ON t1.id = t2.testid
15
19
  and message like '%error%'
16
- where (testid,time) in (select max(time) from testdb.testtable2
20
+ where (t2.testid,t2.time) in (select max(t2.time) from testdb.t2.
17
- group by testid);
21
+ group by t2.testid);
18
22
 
19
23
  試しに上のをやってみましたがうまくいきませんでした。どうすればよろしいでしょうか