相当新的mysql并且无法制定此类查询。
在一个表(名为tableX的表)下,假设结构如下所示
server date numberofUser
server1 timestamp 3
server2 timestamp 2
server3 timestamp 7
server1 timestamp 2
server2 timestamp 8
server3 timestamp 4
......等等
我知道我可以做得很简单 从tableX中选择sum(numberofUser),其中服务器如'server1'按时间戳desc限制100; 获取server1的最后100个时间戳的用户总数
但是你会如何为所有3台服务器执行此操作?我想为每个服务器聚合100个时间戳的所有3台服务器的结果..只是不太确定如何做到这一点。请帮忙。
答案 0 :(得分:1)
使用分组
select sum(numberofUser) , server
from tableX
group by server
并限制您的时间段
select sum(numberofUser) , server
from tableX
where timestamp between 'your_date_min' AND 'your_date_max'
group by server
所有服务器的总数只是
select sum(numberofUser)
from tableX
表示server1和server2的最后10行
单个服务器的总计
select sum(numberofUser), server from (
(select server, date, numberofUser from tableX
where server = 'server1'
order by date limit 10 )
union
(select server, date, numberofUser from tableX
where server = 'server2'
order by date limit 10 )) as t
group by server
或2服务器的总数
select sum(numberofUser) from (
(select server, date, numberofUser from tableX
where server = 'server1'
order by date limit 10)
union
( select server, date, numberofUser from tableX
where server = 'server2'
order by date limit 10 )) as t