字段:d_date 日期
      t_time1起始时间
      t_time2结束时间
若t_time1=18:00,t_time2=6:00则表示是跨天,则查询时t_time2需要d_date +1天作为日期与时间的查询条件如何在一句语句中写 查询t_time1 到t_time2的记录,可能跨天可能不跨天

解决方案 »

  1.   

    在程序里做判断在传入MYSQL中
    If Format(t_time1, "hh:mm:ss") < Format(t_time2, "hh:mm:ss") Then
       'Normal Time
        oTime = " Between '" & Format(Format(d_date, "yyyy-mm-dd") & " " & t_time1, "yyyy-mm-dd hh:mm") & "' " _
                                   & "And '" & Format(Format(d_date, "yyyy-mm-dd") & " " & t_time2, "yyyy-mm-dd hh:mm") & "'"
    Else
                    'Special Time
        oTime = " Between '" & Format(d_date & " " & t_time1, "yyyy-mm-dd hh:mm") & "' " _& "And date_add('" & Format(d_date & " " & t_time2, "yyyy-mm-dd hh:mm") & "',interval 1 day)"
    End If
      

  2.   

    case when t_time2<t_time then d_date+1 end 
      

  3.   

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

  4.   


    贴建表及插入记录的SQL,及要求结果出来看看select * ,CAST(CONCAT(d_date,' ',t_time1)  AS DATETIME) AS newtim1,
    CAST(CONCAT(IF(t_time1>t_time2,d_date+INTERVAL 1 DAY,d_date),' ',t_time2) AS DATETIME) AS newtim2 FROM ttq orselect * from tt where f1 between CAST(CONCAT(d_date,' ',t_time1)  AS DATETIME) and 
    CAST(CONCAT(IF(t_time1>t_time2,d_date+INTERVAL 1 DAY,d_date),' ',t_time2) AS DATETIME)