douzhan8652 2014-03-25 16:14
浏览 102
已采纳

选择MySQL中范围之间的所有时间戳

I'm working on a calendar like feature to a project of mine.

I have a table like the following:

ID | Title             |Where     |tri| Start      | End        | tro
______________________________________________________________________
"4"|"Planingfrenzy"    |"Street 8"|"0"|"1395835200"|"1395846000"|"1"
"5"|"Other meeting"    |"Road 8"  |"0"|"1395140400"|"1395158400"|"1"
"6"|"Third meeting"    |"Lane 8"  |"0"|"1395819000"|"1395824400"|"1"
"8"|"Weekend at cyprus"|"Cyprus"  |"0"|"1395928800"|"1396162800"|"1"

I have a problem selecting alla events that happens in one day. I tried the following two queries, but they only return those events thats start and end at the same day.

/*
   Start is a unixtimestamp for the beginning of the day
   End is a unixtimestamp for the end of the day
*/

//This Returns to many events since all events that ends before the end timestamp is a match etc. 
SELECT * FROM events WHERE (start > ? OR end<?) 

//This matches all events that start and end at the same day. But a multi day event like "Weekend at cyprus" isn't returned since it is out of range
SELECT * FROM events WHERE (start > ? AND end<?)

Is there some way in MySQL or PHP to match if start/end range "touches" in the queried timestamp range?

  • 写回答

3条回答 默认 最新

  • doudu161481 2014-03-25 16:18
    关注

    Assuming that the two values of ? are the earliest and latest values in the day, I think you want:

    where start < (?end) and end > (?start)
    

    That is, there is an overlap when the start of one is before the end of the other, and vice versa.

    In this answer (?end) is intended to be the last timestamp of the day and (?start) is intended to be the first.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题