我写了一个查询如下,但收到错误
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)
特定的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
select * from table3 where run_id IN ('53735', '53736', '53737');
的样本数据
我得到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
最大的id
检查下面的输出
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