数据库中有表A,
表中部分记录如下:
listID listName      createdTime   endedTime     
1      test      2010-7-22 11:23:10  2010-7-22 11:23:23
2      test      2010-7-22 11:23:24  2010-7-23 11:23:57
3      test      2010-7-22 11:23:58  2010-7-23 14:26:57
4      test      2010-7-22 11:26:57  2010-7-23 14:33:01
.        .          .                        .
.        .          .                        .
.        .          .                        .
id  test  2010-7-23 15:23:10  2010-7-23 15:23:50
id  test  2010-7-23 15:23:50  2010-7-23 15:24:30表中记录 时间部分的日期可能连续,也可能不连续,但是时间部分绝对不可能有重合,也就是一个时间点只属于一条记录.
如何查找一个 时间段的sql如:
1)2010-7-22 11:23:12  到2010-7-22 11:27:00  的记录.  结果要求包含记录号(1和4)
2)2010-7-16 11:00:00  到2010-7-22 13:00:00  的记录.  结果要求包含第3条记录.

解决方案 »

  1.   

    再补充下吧.
    就是数据库中 
    记录1的时间段为  A - B
    记录2的时间段为  C - D
    记录3的时间段为  E - F现在查询一个时间段 为 M-N ,M在 A-B之间, N在 E-F之间. 要求查询出来的结果 包含 记录1 和记录3
      

  2.   

     (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式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)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  3.   

    数据库是 mysql5.1create table test
    (
    listID  int(11),
    listName varchar(20),
    createdTime datetime,
    endedTime datetime
    )
    insert into test values (1,'test1','2010-7-22 11:23:10','2010-7-22 11:23:23')
    insert into test values (2,'test2','2010-7-22 11:23:24','2010-7-22 11:23:57')
    insert into test values (3,'test3','2010-7-22 11:23:58','2010-7-22 11:26:57')
    insert into test values (4,'test4','2010-7-22 11:26:58','2010-7-22 11:29:11')
    insert into test values (5,'test5','2010-7-22 11:29:12','2010-7-22 11:29:48')insert into test values (6,'test6','2010-7-24 9:10:46','2010-7-24 9:13:23')
    insert into test values (7,'test7','2010-7-24 9:13:24','2010-7-24 9:15:8')
    insert into test values (8,'test8','2010-7-24 9:15:9','2010-7-24 9:17:23')上面的是我的表的记录.
    问题 如下:
      我需要查询 2010-7-22 11:23:48  到 2010-7-24 8:00:00 的记录. 查询出的结果应该是:记录2到记录5.
    不知道这个查询语句该怎么写.  
      

  4.   

    得到的结果
    如下
    /*
    2,'test2','2010-7-22 11:23:24','2010-7-22 11:23:57'
    3,'test3','2010-7-22 11:23:58','2010-7-22 11:26:57'
    4,'test4','2010-7-22 11:26:58','2010-7-22 11:29:11'
    5,'test5','2010-7-22 11:29:12','2010-7-22 11:29:48'
    /*
      

  5.   

    mysql> select * from test;
    +--------+----------+---------------------+---------------------+
    | listID | listName | createdTime         | endedTime           |
    +--------+----------+---------------------+---------------------+
    |      1 | test1    | 2010-07-22 11:23:10 | 2010-07-22 11:23:23 |
    |      2 | test2    | 2010-07-22 11:23:24 | 2010-07-22 11:23:57 |
    |      3 | test3    | 2010-07-22 11:23:58 | 2010-07-22 11:26:57 |
    |      4 | test4    | 2010-07-22 11:26:58 | 2010-07-22 11:29:11 |
    |      5 | test5    | 2010-07-22 11:29:12 | 2010-07-22 11:29:48 |
    |      6 | test6    | 2010-07-24 09:10:46 | 2010-07-24 09:13:23 |
    |      7 | test7    | 2010-07-24 09:13:24 | 2010-07-24 09:15:08 |
    |      8 | test8    | 2010-07-24 09:15:09 | 2010-07-24 09:17:23 |
    +--------+----------+---------------------+---------------------+
    8 rows in set (0.08 sec)mysql> select * from test
        -> where createdTime<'2010-7-24 8:00:00' and endedTime>'2010-7-22 11:23:48';
    +--------+----------+---------------------+---------------------+
    | listID | listName | createdTime         | endedTime           |
    +--------+----------+---------------------+---------------------+
    |      2 | test2    | 2010-07-22 11:23:24 | 2010-07-22 11:23:57 |
    |      3 | test3    | 2010-07-22 11:23:58 | 2010-07-22 11:26:57 |
    |      4 | test4    | 2010-07-22 11:26:58 | 2010-07-22 11:29:11 |
    |      5 | test5    | 2010-07-22 11:29:12 | 2010-07-22 11:29:48 |
    +--------+----------+---------------------+---------------------+
    4 rows in set (0.03 sec)mysql>
      

  6.   

    还有一种情况是 
    查询  2010-07-22 11:00:00 到 2010-07-24 12:00:00 
    需要的结果
    /*
    1 | test1    | 2010-07-22 11:23:10 | 2010-07-22 11:23:23 |
    2 | test2    | 2010-07-22 11:23:24 | 2010-07-22 11:23:57 |
    3 | test3    | 2010-07-22 11:23:58 | 2010-07-22 11:26:57 |
    4 | test4    | 2010-07-22 11:26:58 | 2010-07-22 11:29:11 |
    5 | test5    | 2010-07-22 11:29:12 | 2010-07-22 11:29:48 |
    6 | test6    | 2010-07-24 09:10:46 | 2010-07-24 09:13:23 |
    7 | test7    | 2010-07-24 09:13:24 | 2010-07-24 09:15:08 |
    8 | test8    | 2010-07-24 09:15:09 | 2010-07-24 09:17:23 |
    */
    这样的怎么写呢?
      

  7.   

    建议一次把问题提完。mysql> select * from test
        -> where createdTime<'2010-07-24 12:00:00' and endedTime>'2010-07-22 11:00:0
    0';
    +--------+----------+---------------------+---------------------+
    | listID | listName | createdTime         | endedTime           |
    +--------+----------+---------------------+---------------------+
    |      1 | test1    | 2010-07-22 11:23:10 | 2010-07-22 11:23:23 |
    |      2 | test2    | 2010-07-22 11:23:24 | 2010-07-22 11:23:57 |
    |      3 | test3    | 2010-07-22 11:23:58 | 2010-07-22 11:26:57 |
    |      4 | test4    | 2010-07-22 11:26:58 | 2010-07-22 11:29:11 |
    |      5 | test5    | 2010-07-22 11:29:12 | 2010-07-22 11:29:48 |
    |      6 | test6    | 2010-07-24 09:10:46 | 2010-07-24 09:13:23 |
    |      7 | test7    | 2010-07-24 09:13:24 | 2010-07-24 09:15:08 |
    |      8 | test8    | 2010-07-24 09:15:09 | 2010-07-24 09:17:23 |
    +--------+----------+---------------------+---------------------+
    8 rows in set (0.00 sec)mysql>