表中有一个表示时间的字段datatime,根据时间排序然后比较表中上下两条记录中datatime的差,如果差值大于10分钟则记录次数+1,如果小于10分钟则记录它的差值,把所有小于10分钟的值都加起来。

解决方案 »

  1.   

     (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  2.   

    先猜一下,估计你想要的是这个。select count(*)
    from (
    select TIMEDIFF(a.一个表示时间的字段datatime,
    (select 一个表示时间的字段datatime from 表 where 一个表示时间的字段datatime<a.一个表示时间的字段datatime order by 一个表示时间的字段datatime desc limit 1)
    ) as d
    from 表 a
    ) t
    where d<1000
      

  3.   

    create table 'tt'(
    id int(11),
    status int(12),
    datatime datetime,
    )INSERT INTO `tt` VALUES ('1216814', '2010-07-27 09:17:13', '128');
    INSERT INTO `tt` VALUES ('1216814', '2010-07-27 09:27:21', '128');
    INSERT INTO `tt` VALUES ('1216814', '2010-07-27 09:17:29', '11');
    INSERT INTO `tt` VALUES ('1216814', '2010-07-27 09:57:20', '11');
    INSERT INTO `tt` VALUES ('1216812', '2010-07-27 09:17:14', '128');
    INSERT INTO `tt` VALUES ('1216812', '2010-07-27 09:17:22', '128');
    INSERT INTO `tt` VALUES ('1216812', '2010-07-27 09:28:30', '128');
    INSERT INTO `tt` VALUES ('1216812', '2010-07-27 09:30:46', '128');结果:
    id datatime status 次数
    1216814 00:00:00 128 2
    1216814 00:09:51 11 1
    1216812 00:02:24 128 2datatime是上下两条记录之间相差时间小于10分钟的和,次数是上下两条记录比较大于10分钟的次数
      

  4.   


    你贴出来的CREATE TABLE, INSERT根本无法运行,语法错。
      

  5.   

    mysql> select * from tt;
    +---------+--------+---------------------+
    | id      | status | datatime            |
    +---------+--------+---------------------+
    | 1216814 |    128 | 2010-07-27 09:17:13 |
    | 1216814 |    128 | 2010-07-27 09:27:21 |
    | 1216814 |     11 | 2010-07-27 09:17:29 |
    | 1216814 |     11 | 2010-07-27 09:57:20 |
    | 1216812 |    128 | 2010-07-27 09:17:14 |
    | 1216812 |    128 | 2010-07-27 09:17:22 |
    | 1216812 |    128 | 2010-07-27 09:28:30 |
    | 1216812 |    128 | 2010-07-27 09:30:46 |
    +---------+--------+---------------------+
    8 rows in set (0.00 sec)
    你期望的结果是怎么出来的? 比如 这个次数,为什么第一行的这个是2?第二行的是1?
    结果:
    id datatime status 次数
    1216814 00:00:00 128 2
    1216814 00:09:51 11 1
    1216812 00:02:24 128 2