-2

喜欢

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; 

我想为此编写单个查询,这可能吗?

4

1 回答 1

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;
于 2019-05-16T05:04:57.013 回答