为日期范围登录的用户选择MySql行,每天返回小时数

时间:2013-01-19 01:50:48

标签: php mysql sql date

我很难写一个SQL查询,希望互联网世界的某些人可以帮助我!

我有一个mysql表,其中包含人们登录和注销时的值,我需要报告日期范围的数据,该日期范围给出了当天每小时登录的人数。

示例所需输出:

2013-1-10 00:00 5
2013-1-10 01:00 13
... for every hour of 2013-1-10 ending at 23:00...
2013-1-10 23:00 23
...and then the same for every date in the range...
2013-1-11 00:00 4
2013-1-11 01:00 6

我可以编写查询按日期对所有内容进行分组,并根据登录时间获取每小时的计数,问题是如果人们登录或超过一小时,他们的计数将不会显示在第二个小时,因为我按DATE_FORMAT分组(time_login,'%H')。

示例查询在此处:

SELECT mac, 
DATE_FORMAT(time_login, '%H') AS hour_logged_in,
DATE_FORMAT(time_logout, '%H') AS hour_logged_out,
DATE_FORMAT(time_login, '%Y-%m-%d') as date,
count(*) as dealcounts 
FROM sessions 
WHERE (
    time_logout IS NOT NULL 
     ) 
AND (
    DATE_FORMAT(time_login, '%Y-%m-%d') BETWEEN 
    DATE_FORMAT('2013-1-1', '%Y-%m-%d') AND DATE_FORMAT('2013-1-18', '%Y-%m-%d')) 
GROUP BY hour_logged_in

有谁知道如何获得每天每小时登录的行数?

我在这里设置了一个sqlfiddle:http://www.sqlfiddle.com/#!2/e13a0/7,带有上述查询

额外信用:没有人登录的小时空值: - )

很高兴为贝宝提供一些啤酒和咖啡给任何可以帮助的人! 谢谢!

1 个答案:

答案 0 :(得分:1)

我认为下面的mysql代码接近你要找的东西?

备注

  1. 它不优雅,除非它优雅的方式可以让一个主要的黑客优雅。第一眼看上去可能会导致中度到严重的肠/脑痉挛而不是它的粗糙。

  2. 我需要添加索引等实际用途。

  3. 请注意,我将会话表的名称更改为 sessions_example 。如果执行此操作,我不想破坏会话表。

  4. 最后,在名为 hours_expanded 的新表格中查找结果。

  5. 需要更多测试。我现在已经疲惫不堪,所以我正在提供潜力 解。我认为一般方法是正确的,代码似乎正确,但验证!

  6. 我通过将登录数量按小时扩展到当前小时(NOW())来处理主动登录记录。如果有人可以在没有注销的情况下注销总是进入会话表,那么您可能需要做一些不同的事情。即为标记为活动的记录设置最大截止时间。

  7. 一般方法

    基本上,当我阅读这个问题时,我知道我的首选方法是“扩展”所有涉及实际表格行的时间。我发现这在测试和理解这样的情况下更为可取,尽管它不一定是一个很好的表演者。在我弄清楚如何做到这一点的时候,我发现了一篇博文http://datacharmer.blogspot.com/2007/12/data-from-nothing-solution-to-pop-quiz.html,(参见Roland Bouman和Kai Voigt的参考文献)真的,真的帮助我获得了我想要的行扩展。我习惯在MSSQL中用CTE做这种事情 - 所以我需要帮助找到一种在MySQL中生成任意行数的方法。

    这里是:

    /* Following test data from provided sql fiddle */
    
    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    /*!40101 SET NAMES utf8 */;
    
    /*changed name of sessions table to sessions_example, so as not to interfere with any prior sessions table*/
    DROP TABLE IF EXISTS sessions_example;
    CREATE TABLE `sessions_example` (
      `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
      `user_id` int(11) NOT NULL,
      `network_id` int(11) NOT NULL,
      `mac` varchar(17) NOT NULL,
      `time_login` datetime NOT NULL,
      `time_logout` datetime DEFAULT NULL,
      `data_in` bigint(20) DEFAULT NULL,
      `data_out` bigint(20) DEFAULT NULL,
      `sessionid` varchar(20) NOT NULL,
      `user_ip` varchar(20) DEFAULT NULL,
      `external_ip` varchar(20) DEFAULT NULL,
      `opfield1` varchar(255) DEFAULT NULL,
      `opfield2` varchar(255) DEFAULT NULL,
      `opfield3` varchar(255) DEFAULT NULL,
      `txtfield4` text,
      PRIMARY KEY (`id`),
      UNIQUE KEY `id` (`id`),
      KEY `sessionid` (`sessionid`),
      KEY `time_login` (`time_login`),
      KEY `time_logout` (`time_logout`),
      KEY `ap_id` (`network_id`),
      KEY `user_id` (`user_id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1009253406 ;
    
    INSERT INTO `sessions_example` (`id`, `user_id`, `network_id`, `mac`, `time_login`, `time_logout`, `data_in`, `data_out`, `sessionid`, `user_ip`, `external_ip`, `opfield1`, `opfield2`, `opfield3`, `txtfield4`) VALUES
    (1009250911, 0, 254, 'F8-7B-7A-AC-EA-D6', '2013-01-07 15:45:05', '2013-01-07 16:55:05', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009250912, 0, 254, '00-88-65-D9-B1-8E', '2013-01-07 15:50:04', '2013-01-07 19:11:58', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009250914, 0, 254, 'D0-23-DB-17-DC-FA', '2013-01-07 16:20:04', '2013-01-07 17:35:05', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009250915, 0, 254, 'F8-7B-7A-AC-EA-D6', '2013-01-07 16:25:01', '2013-01-07 16:50:04', NULL, NULL, 'active', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009250917, 0, 254, 'E0-B9-BA-F0-45-83', '2013-01-07 16:45:05', '2013-01-07 19:11:58', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009250919, 0, 254, 'F8-7B-7A-AC-EA-D6', '2013-01-07 16:55:05', '2013-01-07 17:12:30', NULL, NULL, 'active', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009250920, 0, 254, 'F8-7B-7A-AC-EA-D6', '2013-01-07 17:12:30', '2013-01-07 17:15:06', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009250921, 0, 254, 'F8-7B-7A-AC-EA-D6', '2013-01-07 17:15:06', '2013-01-07 19:11:59', NULL, NULL, 'active', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009250926, 0, 254, 'F8-7B-7A-AC-EA-D6', '2013-01-07 19:11:59', '2013-01-07 20:30:04', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009250927, 0, 254, '00-1B-9E-75-3E-DA', '2013-01-07 19:11:59', '2013-01-07 20:45:04', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009250929, 0, 254, '00-88-65-D9-B1-8E', '2013-01-07 20:35:04', '2013-01-07 22:15:04', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009250930, 0, 254, 'F8-7B-7A-AC-EA-D6', '2013-01-07 20:50:04', '2013-01-07 22:25:04', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009250931, 0, 254, '00-26-C7-CD-10-9C', '2013-01-07 21:10:04', NULL, NULL, NULL, 'active', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009250933, 0, 254, '00-1B-9E-75-3E-DA', '2013-01-07 21:30:04', '2013-01-08 00:20:04', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009250934, 0, 254, '00-88-65-D9-B1-8E', '2013-01-07 23:30:04', '2013-01-08 00:05:04', NULL, NULL, 'active', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009250935, 0, 254, 'F8-7B-7A-AC-EA-D6', '2013-01-07 23:50:04', '2013-01-08 00:50:05', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009250936, 0, 254, '00-88-65-D9-B1-8E', '2013-01-08 00:05:04', '2013-01-08 02:20:05', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009250940, 0, 254, '00-26-C7-CD-10-9C', '2013-01-08 01:10:05', '2013-01-08 02:10:05', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009250941, 0, 254, '5C-0A-5B-5E-5D-C3', '2013-01-08 01:25:05', '2013-01-08 02:25:06', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009250942, 0, 254, '00-88-65-D9-B1-8E', '2013-01-08 03:00:06', '2013-01-08 05:45:07', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009250943, 0, 254, '10-40-F3-6E-D0-FA', '2013-01-08 03:05:07', '2013-01-08 04:45:06', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009251100, 0, 254, '28-CF-DA-D9-30-66', '2013-01-08 03:20:06', '2013-01-08 05:10:06', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009251101, 0, 253, '00-26-AB-00-E0-1F', '2013-01-08 03:45:03', '2013-01-11 05:15:12', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009251102, 0, 253, '8C-A9-82-85-EF-72', '2013-01-08 03:45:03', '2013-01-11 05:25:15', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009251103, 0, 253, 'F0-CB-A1-43-A8-4D', '2013-01-08 04:05:03', '2013-01-11 05:15:09', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009251104, 0, 254, '00-1B-9E-75-3E-DA', '2013-01-08 04:10:07', '2013-01-08 05:10:07', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009251105, 0, 254, 'F8-7B-7A-AC-EA-D6', '2013-01-08 04:20:06', '2013-01-08 06:35:05', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009251106, 0, 253, '90-18-7C-A9-0B-2B', '2013-01-08 04:30:03', '2013-01-11 05:15:14', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009251107, 0, 253, '30-F7-C5-C5-9F-61', '2013-01-08 04:30:03', '2013-01-11 05:15:15', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009251108, 0, 253, '70-DE-E2-32-F8-66', '2013-01-08 04:30:03', '2013-01-11 09:55:15', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009251109, 0, 254, '00-26-C7-CD-10-9C', '2013-01-08 04:55:07', '2013-01-08 06:00:05', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009251110, 0, 254, '5C-0A-5B-A7-54-20', '2013-01-08 05:15:07', '2013-01-08 06:15:05', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009251111, 0, 253, '78-A3-E4-E7-0F-1C', '2013-01-08 05:20:03', '2013-01-11 05:15:06', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009251112, 0, 254, '00-1B-9E-75-3E-DA', '2013-01-08 05:35:08', '2013-01-08 06:45:05', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009251113, 0, 253, '00-1F-F3-FA-01-43', '2013-01-08 06:35:04', '2013-01-11 05:15:14', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009251114, 0, 253, 'D8-B3-77-7A-C3-F2', '2013-01-08 06:40:03', '2013-01-11 05:15:13', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL),
    (1009251128, 0, 254, '00-88-65-D9-B1-8E', '2013-01-08 16:25:08', '2013-01-08 17:40:05', NULL, NULL, 'passive', NULL, NULL, NULL, NULL, NULL, NULL);
    
    /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
    /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
    /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
    
    /* end of provided sql fiddle data */
    
    #Here is where we set up a table to hold expansion of all hours in rage as unix timestamp values.
    DROP TABLE IF EXISTS hours_expanded;
    CREATE TABLE hours_expanded
    (
         id int auto_increment,
         hour_as_unix_ts int,
         full_datetime datetime NULL,
         hour_as_time time NULL,
         count_logged_in int DEFAULT 0,
         PRIMARY KEY (id)
    );
    
    #get min/max hours
    SELECT @min_in := floor(unix_timestamp(min(time_login))/3600), 
        @max_out := floor(unix_timestamp(NOW())/3600)
    FROM sessions_example;
    
    #total number of rows that we will need in the `hours_expanded` table
    
    SELECT @hours_in_sessions:= @max_out - @min_in + 1;
    
    #dial up packet length for the string based row-generation solution (see tip of the hat)
    SET @old_max_packet = @@max_allowed_packet;
    SET GLOBAL max_allowed_packet = 1073741824;
    
    #Next, we will create a long UNION ALL string that will create one row per hour needed.
    #For the following concat based approach major tip of the hat to
    #http://datacharmer.blogspot.com/2007/12/data-from-nothing-solution-to-pop-quiz.html
    #and Roland Bouman who is mentioned there as proposing this solution to generating rows.
    SET @one_row_per_hour = (SELECT concat("INSERT INTO hours_expanded (hour_as_unix_ts) ",(repeat(concat(@s:="select 1 ","union all "),@hours_in_sessions)),"select 1;"));
    
    #dial packet length back down
    SET GLOBAL max_allowed_packet = @old_max_packet;
    
    #make that a prepared statement via the string and execute
    prepare q from @one_row_per_hour;
    execute q;
    
    #set hour_as_unix_ts column to actual UNIX TIMESTAMP hour value and full_datetime to show datetime for that hour
    UPDATE hours_expanded SET hour_as_unix_ts = (SELECT @min_in * 3600 + 3600 *( id - 1)),
        full_datetime = FROM_UNIXTIME(hour_as_unix_ts),
        hour_as_time = time(full_datetime);
    
    #get the counts and update the hours table
    UPDATE hours_expanded h
    INNER JOIN
    (
        SELECT h.hour_as_unix_ts, COUNT(*) as the_count
        FROM hours_expanded h
        INNER JOIN sessions_example
        WHERE h.hour_as_unix_ts >= floor(unix_timestamp(time_login)/3600)*3600 and h.hour_as_unix_ts <= floor(unix_timestamp(ifnull(time_logout,now()))/3600)* 3600
        GROUP by h.hour_as_unix_ts 
    ) t on h.hour_as_unix_ts = t.hour_as_unix_ts SET h.count_logged_in = t.the_count;
    
    #null out any hours that have a count of 0
    UPDATE hours_expanded SET full_datetime = NULL, hour_as_time = NULL WHERE count_logged_in = 0;
    
    #Final Select with desired output (reformat full_datetime and hour_as_time as desired)
    SELECT full_datetime, hour_as_time, count_logged_in FROM hours_expanded WHERE full_datetime IS NOT NULL
    

    关于字符串concat / repeat 的最后注释。

    1000000行小时,涵盖超过110年的时间,需要13.5M字符串“SELECT 1 UNION ALL SELECT 1 ...”等。

    因此,尽管该方法涉及使用大字符串来获取行扩展,但字符串大小对于所涉及的问题的规模并不坏 - 除非您有几千年的会话日志要处理: - }。

    我确实将max_packet_size临时增加到最大值(1G),以确保不会出现问题 - 过度杀伤,具有很大的安全范围。我不再需要将max_packet_size减少回系统设置。