有可能使用内部查询的输出来输入外部查询的原因吗?

时间:2019-05-16 04:52:42

标签: mysql hive hivecli

喜欢

select ho from (select sourceaddress,count(sourceaddress) as 
src,hour(eventtime) as ho 
from default.fullandfinal 
where sourceaddress='0.0.0.0' and  eventtime between '2019-05-11 00:00:00' and 
'2019-05-11 19:59:59'  
group by sourceaddress,hour(eventtime) order by sourceaddress,ho) t where 
src=28350;

此查询的输出为 11 ,我想在我的nxt查询中使用此输出

select sourceaddress,destinationaddress,destinationport,name,count(*) as count  
from fullandfinal 
where eventtime like "11%" and sourceaddress='0.0.0.0'  
group by sourceaddress,destinationaddress,destinationport,name 
order by count desc limit 5; 

我想为此编写单个查询吗?

1 个答案:

答案 0 :(得分:0)

考虑使用MySQL-可以。您必须将第一个查询用作第二个查询中的子查询。查询结构将是这样的-

SELECT *,
(SELECT ID FROM TABLE_1 WHERE ....) AS [From Other Query] -- In the selection part 
FROM TABLE_2
WHERE TABLE_2.ID = (SELECT ID FROM TABLE_1 WHERE ....) -- In Where condition

对于上述两种情况,您都必须确保子查询返回的单个值类似于您提到的11。

尝试一下-

SELECT sourceaddress,
destinationaddress,
destinationport,
[name],
COUNT(*) as [count]  
FROM fullandfinal 
WHERE 
eventtime LIKE
(
    SELECT ho FROM 
    (
        SELECT sourceaddress,
        COUNT(sourceaddress) AS src,
        HOUR(eventtime) AS ho 
        FROM DEFAULT.fullandfinal 
        WHERE sourceaddress='0.0.0.0' 
        AND  eventtime BETWEEN '2019-05-11 00:00:00' AND '2019-05-11 19:59:59'  
        GROUP BY sourceaddress,
        HOUR(eventtime) 
        ORDER BY sourceaddress,ho
    ) t
    WHERE src=28350
) + '%' 
AND sourceaddress='0.0.0.0'  
GROUP BY sourceaddress,destinationaddress,destinationport,name 
ORDER BY COUNT(*) DESC 
limit 5;