起始时间 和 结束时间 在数据库中有单独字段存储。当前时间是 得到系统当前时间。数据库中的数据如下:
+-----+-----------------------------+-----------------------------+----------+
| gid | gstarttime                   | gstoptime                    | gaddress |
+-----+-----------------------------+-----------------------------+----------+
|   1 | 2011-08-25 09:12:35 | 2011-08-25 09:12:35 | 1        |
|   2 | 2011-08-25 09:33:59 | 2011-08-25 09:33:59 | 2        |
|   3 | 2011-08-25 09:36:31 | 2011-08-25 09:36:31 | 3        |
|   4 | 2011-08-25 11:02:07 | 2011-09-25 11:02:07 | 5        |
|   5 | 2011-08-25 16:09:18 | 2011-09-23 16:09:18 | 5        |
|   6 | 2011-08-31 11:23:36 | 2011-09-30 11:23:36 | 5        |
+-----+---------------------+---------------------+----------+我只想要  4 | 2011-08-25 11:02:07 | 2011-09-25 11:02:07 | 5        |
这条数据。就是说我要查询在当前时间是否在开始时间和结束时间之内,如果是,则显示出来这条数据,而且只要显示正序第一条。
我想要的是用SQl语句直接的查询出来我想要的那条数据。

解决方案 »

  1.   

    mysql> select * 
    from tb5 
    where now() between gstarttime and gstoptime
    limit 1;
    +-----+---------------------+---------------------+----------+
    | gid | gstarttime          | gstoptime           | gaddress |
    +-----+---------------------+---------------------+----------+
    |   4 | 2011-08-25 11:02:07 | 2011-09-25 11:02:07 |        5 |
    +-----+---------------------+---------------------+----------+
    1 row in setmysql> select now();
    +---------------------+
    | now()               |
    +---------------------+
    | 2011-08-27 13:47:46 |
    +---------------------+
    1 row in setmysql> 
      

  2.   

     select  gid, gstarttime,gstoptime,gaddress from guanggao where (now() between gstarttime and gstoptime) and gaddress=5 limit 1;
      

  3.   

     select  gid, gstarttime,gstoptime,gaddress from guanggao where (now() between gstarttime and gstoptime) and gaddress=5 limit 1;