hi, everybody.
  有个sql问题无法解决,想请教一下。
问题是:
数据库是 mysql
有一个表。tb_login_log.
3个字段  
uid(int)
login_time(TIMESTAMP)
type(int)
(忽略设计上存在的问题)用户每次登陆都产生一条日志。
下面是表的一些数据
100000 2011-07-14 00:50:04
100000 2011-07-16 00:50:08
100000 2011-07-17 00:50:19100001 2011-07-15 00:50:28
100001 2011-07-15 00:50:32
100001 2011-07-16 00:50:37
100001 2011-07-17 00:50:59100002 2011-07-16 01:56:27
100002 2011-07-17 01:56:27
获取所有的登录数据的语句是 select* from tb_login_log where type= 0我现在有一个日期,这个日期一般是当天
我想要的数据时,这个日期之前(包括这个日期),每个用户连续登陆(每天)的日期最小值。如果日期是 2011-07-17,数据是上面的数据的话,那么结果就是100000 2011-07-16
100001 2011-07-15
100002 2011-07-16写不出sql语句,又不想用程序解决。
求帮助,谢谢!

解决方案 »

  1.   

    假设你的数据是:
    100000 2011-07-14 00:50:04
    100000 2011-07-16 00:50:08
    100000 2011-07-17 00:50:19100001 2011-07-15 00:50:28
    100001 2011-07-15 00:50:32
    100001 2011-07-16 00:50:37
    100001 2011-07-17 00:50:59100002 2011-07-16 01:56:27
    100002 2011-07-17 01:56:27select * from tt a where not exists(select 1 from tt where a.uid=uid and a.login_time>login_time)100000的时间 应该是2011-07-14 00:50:04?
      

  2.   

    贴建表及插入记录的SQL,及要求结果出来看看
    数据要有代表性
      

  3.   

    CREATE TABLE `NewTable` (
    `uid`  int(11) NOT NULL ,
    `action_time`  timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ,
    `type`  tinyint(4) NOT NULL COMMENT '0 表示 login 1表示logout,2表示 abnormal logout' ,
    INDEX `idx_uid` (`uid`) USING BTREE ,
    INDEX `idx_action_time` (`action_time`) USING BTREE 
    )
    ENGINE=InnoDB
    DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
    ROW_FORMAT=COMPACT
    ;
      

  4.   

    准备数据,20-30条,要有代表性要,用MYSQLDUMP导出
      

  5.   


    INSERT INTO `tb_log_login_logout` VALUES (100000, '2011-7-24 14:25:43', 2);
    INSERT INTO `tb_log_login_logout` VALUES (100000, '2011-7-25 14:26:41', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100000, '2011-7-26 14:29:36', 2);
    INSERT INTO `tb_log_login_logout` VALUES (100000, '2011-7-27 14:29:38', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100000, '2011-7-28 14:33:18', 0);INSERT INTO `tb_log_login_logout` VALUES (100001, '2011-7-25 14:43:24', 2);
    INSERT INTO `tb_log_login_logout` VALUES (100001, '2011-7-26 14:45:22', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100001, '2011-7-28 14:45:45', 2);
    INSERT INTO `tb_log_login_logout` VALUES (100001, '2011-7-28 14:45:52', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100001, '2011-7-28 14:47:52', 2);
    INSERT INTO `tb_log_login_logout` VALUES (100001, '2011-7-28 14:50:25', 0);INSERT INTO `tb_log_login_logout` VALUES (100002, '2011-7-27 14:53:42', 2);
    INSERT INTO `tb_log_login_logout` VALUES (100002, '2011-7-28 14:54:20', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100002, '2011-7-28 14:55:52', 2);
    INSERT INTO `tb_log_login_logout` VALUES (100002, '2011-7-28 14:56:05', 0);INSERT INTO `tb_log_login_logout` VALUES (100003, '2011-7-26 15:01:33', 2);
    INSERT INTO `tb_log_login_logout` VALUES (100003, '2011-7-26 15:41:07', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100003, '2011-7-28 15:43:23', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100003, '2011-7-28 15:47:26', 2);
    INSERT INTO `tb_log_login_logout` VALUES (100003, '2011-7-28 15:47:40', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100003, '2011-7-28 15:49:43', 2);
      

  6.   

    错了
    重发。
    INSERT INTO `tb_log_login_logout` VALUES (100000, '2011-7-24 14:25:43', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100000, '2011-7-25 14:26:41', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100000, '2011-7-26 14:29:36', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100000, '2011-7-27 14:29:38', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100000, '2011-7-28 14:33:18', 0);INSERT INTO `tb_log_login_logout` VALUES (100001, '2011-7-25 14:43:24', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100001, '2011-7-26 14:45:22', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100001, '2011-7-28 14:45:45', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100001, '2011-7-28 14:45:52', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100001, '2011-7-28 14:47:52', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100001, '2011-7-28 14:50:25', 0);INSERT INTO `tb_log_login_logout` VALUES (100002, '2011-7-27 14:53:42', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100002, '2011-7-28 14:54:20', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100002, '2011-7-28 14:55:52', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100002, '2011-7-28 14:56:05', 0);INSERT INTO `tb_log_login_logout` VALUES (100003, '2011-7-26 15:01:33', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100003, '2011-7-26 15:41:07', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100003, '2011-7-28 15:43:23', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100003, '2011-7-28 15:47:26', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100003, '2011-7-28 15:47:40', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100003, '2011-7-28 15:49:43', 0);
      

  7.   

    INSERT INTO `tb_log_login_logout` VALUES (100001, '2011-7-25 14:43:24', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100001, '2011-7-26 14:45:22', 0);
    //这里隔了一天,就不能算是连续登陆,26,28不连续
    INSERT INTO `tb_log_login_logout` VALUES (100001, '2011-7-28 14:45:45', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100001, '2011-7-28 14:45:52', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100001, '2011-7-28 14:47:52', 0);
    INSERT INTO `tb_log_login_logout` VALUES (100001, '2011-7-28 14:50:25', 0);