我写了一个如下查询,但收到错误
SELECT DISTINCT T2.host,
T3.error
FROM table1 T1
INNER JOIN table2 T2
ON
T1.item_id = T2.item_id
INNER JOIN table3 T3
ON
T1.ID = T3.run_id
and T1.submit_id = (select max(T1.submit_id) from table1 T1)
;
此处T3.run_id = (select max(T3.run_id) from T3)
发出错误
对于特定T3
max(T3.run_id)
的{{1}}有三行
T1.item_id = T2.item_id
以上是item_id host
15907 abc.com
7303 cde.com
7304 abcd.com
7305 cdedf.com
的示例数据
table 2
select * from table2 where item_id = '15907';
以上是id submit_id item_id
49898 16693 15907
49899 16693 15907
49900 16693 15907
53735 17972 15907
53736 17972 15907
53737 17972 15907
的示例数据
table 1
上面我需要选择select * from table1 where item_id = '15907';
和max(submit_id)
的行
在此示例中,它将是T1.item_id = T2.item_id
17972
以上是id run_id error
12345 53735 error1
12345 53735 error2
12346 53735 error3
12347 53736 error4
12348 53736 error5
12349 53737 error6
的示例数据
table3
我得到0行作为输出
答案 0 :(得分:0)
您可以按如下所述重写查询以获得预期的输出
SELECT DISTINCT T2.host,T3.error
FROM table2 T2
INNER JOIN (
SELECT a.*
FROM table1 a
LEFT JOIN table1 b ON a.item_id = b.item_id AND a.submit_id < b.submit_id
WHERE b.submit_id IS NULL
) T1 ON T1.item_id = T2.item_id
INNER JOIN table3 T3 ON T1.ID = T3.run_id
我已在内部查询中移动了table1以获取submit_id
最大值
答案 1 :(得分:0)
检查下面的输出
SELECT DISTINCT T2.host
,T3.error
FROM table1 T1
INNER JOIN table2 T2 ON T1.item_id = T2.item_id
and T1.submit_id = (select max(T1.submit_id) from table1 T1 )
INNER JOIN table3 T3 ON T1.ID = T3.run_id
;
输出
host error
abc.com error1
abc.com error2
abc.com error3
abc.com error4
abc.com error5
abc.com error6