MYSQL每小时​​数据内连接

时间:2015-12-30 12:28:29

标签: mysql select count group-by hour

表用户

username |   name   | role
admin    | tony     | 1
sales    | roy      | 2
custo    | boy      | 3

表logstat

logdate             |   ip              | username
2015/12/12 18:02:22 | 192.168.1.11      | admin
2015/11/12 21:02:44 | 192.168.1.101     | sales
2015/11/12 22:04:27 | 192.168.1.121     | custo

我想要的输出是:

datetime              | admin   | sales     | custo
(currentdate) 18:00:0 | 1       | 0         | 0
(currentdate) 21:00:0 | 0       | 1         | 0
(currentdate) 21:00:0 | 0       | 0         | 1

输出为什么我需要(currentdate)在显示系统中每个角色的每小时数据时使用ARMCHART需求进行调整。

到目前为止,我被困在

SELECT CONCAT(Curdate(), ' ', HOUR(logdate)) as 'hourly', 
(select count('logstat.username') 
   from logstat inner join user on user.username=logstat.username 
   and user.idrole='1' 
   where logdate BETWEEN (CURDATE() + INTERVAL (SELECT hourly(NOW())) hourly - INTERVAL 23 hourly) 
   AND ((CURDATE() + INTERVAL (SELECT hourly(NOW())) hourly)))as 'Admin' 
   FROM `logstat` GROUP BY `hourly`

1 个答案:

答案 0 :(得分:0)

我目前发现了这个代码,

SELECT CONCAT(Curdate(),' ',DATE_FORMAT(logdate,'%H')) as 'hourly',
     (select count('logstat.username') from logstat inner join user on user.username=logstatus.username and user.idrole='1' where HOUR(logdate) = HOUR(hourly))as 'Admin',
    (select count('logstat.username') from logstat inner join user on user.username=logstatus.username and user.idrole='2' where HOUR(logdate) = HOUR(hourly))as 'Sales',
    (select count('logstat.username') from logstat inner join user on user.username=logstatus.username and user.idrole='3' where HOUR(logdate) = HOUR(hourly))as 'Custo'   
FROM `logstat` GROUP BY `hourly`

希望帮助使用amchart。