dongmaqiu6084 2015-07-15 21:30
浏览 49
已采纳

如果日期在表中的现有日期之间,则为Mysql

I'm trying to get a listing of everything between two dates, including those items with start and end dates before and after a specific date.

Example:

SELECT * FROM expenses WHERE (userid = '#' AND exstartdate >= '2015-07-25' 
AND exenddate <= '2015-08-24') OR (userid = '#' AND reg='Yes') 
ORDER BY eamount DESC LIMIT 0, 25

eid ename           eamount userid  exstartdate exenddate   paid    reg
1   Bill 1          1000        #   2015-01-01  2015-11-01  no      Yes
17  bill 17         300         #   2015-07-29  2015-07-29  no      No
2   Bill 2          85          #   2013-11-01  2017-12-31  no      Yes
6   Lunches         80          #   2015-01-01  2016-12-31  no      Yes

I'm trying to get this in as well:

3   Bill 3         87.00    1   Yes 2015-01-01  2016-05-01  no  No

but can't seem to do it unless I reverse the > and <. Bills 1, 2 and lunch are showing up because of the reg flag.

I'm wondering if this would be better to do with PHP instead.

Edit: To clarify, I'm looking to get all items that are between $date1 (Seen above as 2015-07-25) and $date2 (2015-05-24) from the table. The table has start dates and end dates that if the date from the table includes the date listed in $date1 and $date2.

So if $date1(2015-07-25) is after exstartdate(2015-01-01) and $date2(2015-08-24) is before exenddate(2015-11-01) then it shows the result.

Also show if exstartdate and exenddate is between $date1 and $date2.

Basically, if the dates between exstartdate and exenddate also match a date between $date1 and $date2, then show result.

  • 写回答

3条回答 默认 最新

  • doujiexin1136 2015-07-15 21:40
    关注

    According to your description you want:

    SELECT * FROM expenses 
    WHERE userid = '#' AND 
        ((exstartdate >= '2015-07-25' AND exenddate <= '2015-08-24') OR 
         (exstartdate < '2015-07-25' AND exenddate > '2015-08-24') OR 
         reg='Yes') 
    ORDER BY eamount DESC LIMIT 0, 25
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能