首页 \ 问答 \ 在MySQL中查找'免费'时间(Finding 'free' times in MySQL)

在MySQL中查找'免费'时间(Finding 'free' times in MySQL)

我有一张桌子如下:

mysql> DESCRIBE student_lectures;
+------------------+----------+------+-----+---------+----------------+
| Field            | Type     | Null | Key | Default | Extra          |
+------------------+----------+------+-----+---------+----------------+
| id               | int(11)  | NO   | PRI | NULL    | auto_increment |
| course_module_id | int(11)  | YES  | MUL | NULL    |                |
| day              | int(11)  | YES  |     | NULL    |                |
| start            | datetime | YES  |     | NULL    |                |
| end              | datetime | YES  |     | NULL    |                |
| cancelled_at     | datetime | YES  |     | NULL    |                |
| lecture_type_id  | int(11)  | YES  |     | NULL    |                |
| lecture_id       | int(11)  | YES  |     | NULL    |                |
| student_id       | int(11)  | YES  |     | NULL    |                |
| created_at       | datetime | YES  |     | NULL    |                |
| updated_at       | datetime | YES  |     | NULL    |                |
+------------------+----------+------+-----+---------+----------------+

我基本上想找到一次演讲没有发生的时间 - 所以要做到这一点,我正在考虑将重叠讲座分组在一起(例如,上午9点 - 10点和上午10点 - 上午11点的演讲将显示为单班9 am-11am讲座)。 可能会有两个以上的讲座背靠背。

我目前有这样的:

SELECT l.start, l2.end
FROM student_lectures l
LEFT JOIN student_lectures l2 ON ( l2.start = l.end )
WHERE l.student_id = 1 AND l.start >= '2010-04-26 09:00:00' AND l.end <= '2010-04-30 19:00:00' AND l2.end IS NOT NULL AND l2.end != l.start
GROUP BY l.start, l2.end
ORDER BY l.start, l2.start

其中返回:

+---------------------+---------------------+
| start               | end                 |
+---------------------+---------------------+
| 2010-04-26 09:00:00 | 2010-04-26 11:00:00 |
| 2010-04-26 10:00:00 | 2010-04-26 12:00:00 |
| 2010-04-26 10:00:00 | 2010-04-26 13:00:00 |
| 2010-04-26 13:15:00 | 2010-04-26 16:15:00 |
| 2010-04-26 14:15:00 | 2010-04-26 16:15:00 |
| 2010-04-26 15:15:00 | 2010-04-26 17:15:00 |
| 2010-04-26 16:15:00 | 2010-04-26 18:15:00 |
...etc...

我从中找到的输出是:

+---------------------+---------------------+
| start               | end                 |
+---------------------+---------------------+
| 2010-04-26 09:00:00 | 2010-04-26 13:00:00 |
| 2010-04-26 13:15:00 | 2010-04-26 18:15:00 |

任何帮助表示感谢,谢谢!


I've got a table as follows:

mysql> DESCRIBE student_lectures;
+------------------+----------+------+-----+---------+----------------+
| Field            | Type     | Null | Key | Default | Extra          |
+------------------+----------+------+-----+---------+----------------+
| id               | int(11)  | NO   | PRI | NULL    | auto_increment |
| course_module_id | int(11)  | YES  | MUL | NULL    |                |
| day              | int(11)  | YES  |     | NULL    |                |
| start            | datetime | YES  |     | NULL    |                |
| end              | datetime | YES  |     | NULL    |                |
| cancelled_at     | datetime | YES  |     | NULL    |                |
| lecture_type_id  | int(11)  | YES  |     | NULL    |                |
| lecture_id       | int(11)  | YES  |     | NULL    |                |
| student_id       | int(11)  | YES  |     | NULL    |                |
| created_at       | datetime | YES  |     | NULL    |                |
| updated_at       | datetime | YES  |     | NULL    |                |
+------------------+----------+------+-----+---------+----------------+

I'm essentially wanting to find times when a lecture doesn't happen - so to do this I'm thinking a query to group overlapping lectures together (so, for example, 9am-10am and 10am-11am lectures will be shown as a single 9am-11am lecture). There may be more than two lectures back-to-back.

I've currently got this:

SELECT l.start, l2.end
FROM student_lectures l
LEFT JOIN student_lectures l2 ON ( l2.start = l.end )
WHERE l.student_id = 1 AND l.start >= '2010-04-26 09:00:00' AND l.end <= '2010-04-30 19:00:00' AND l2.end IS NOT NULL AND l2.end != l.start
GROUP BY l.start, l2.end
ORDER BY l.start, l2.start

Which returns:

+---------------------+---------------------+
| start               | end                 |
+---------------------+---------------------+
| 2010-04-26 09:00:00 | 2010-04-26 11:00:00 |
| 2010-04-26 10:00:00 | 2010-04-26 12:00:00 |
| 2010-04-26 10:00:00 | 2010-04-26 13:00:00 |
| 2010-04-26 13:15:00 | 2010-04-26 16:15:00 |
| 2010-04-26 14:15:00 | 2010-04-26 16:15:00 |
| 2010-04-26 15:15:00 | 2010-04-26 17:15:00 |
| 2010-04-26 16:15:00 | 2010-04-26 18:15:00 |
...etc...

The output I'm looking for from this would be:

+---------------------+---------------------+
| start               | end                 |
+---------------------+---------------------+
| 2010-04-26 09:00:00 | 2010-04-26 13:00:00 |
| 2010-04-26 13:15:00 | 2010-04-26 18:15:00 |

Any help appreciated, thanks!


原文:https://stackoverflow.com/questions/2798249
更新时间:2024-04-22 18:04

最满意答案

比如说,

begin
@user = User.find(params[:id])
rescue ActiveRecord::RecordNotFound
  flash[:notice] = "#No such record in User for id :: {params[:id]} on #{action_name}"
end

UPDATE

flash[:notice] = t('flash.recordnotfound',:class_name => self.class.name, :column_name => params[:id], :action_name => action_name)

现在在你的config/locales/en.yml (这有助于翻译,请参考i18n)

flash:
  recordnotfound:  "Sorry, no record od %{column_name} in class %{class_name} was found on you action %{action_name}"

如果您不想使用区域设置,只需将此信息放在flash[:notice]

更有活力?

写一个函数并在那里使用相同的flash [:notice]。 不要伤害。

想要更多数据?

这是一个快速的解决方案,我总是在我的观点中<%= params%> ,以便轻松了解最新情况以及最新消息。 然后,您可以打开rails控制台并播放不同的操作等。

user = User.new
user.save
user.errors.messages

我认为所有这些都是足够好的数据。

祝你好运。


Say for example,

begin
@user = User.find(params[:id])
rescue ActiveRecord::RecordNotFound
  flash[:notice] = "#No such record in User for id :: {params[:id]} on #{action_name}"
end

UPDATE

flash[:notice] = t('flash.recordnotfound',:class_name => self.class.name, :column_name => params[:id], :action_name => action_name)

Now in your config/locales/en.yml (this would help translate, refer to i18n here)

flash:
  recordnotfound:  "Sorry, no record od %{column_name} in class %{class_name} was found on you action %{action_name}"

If you do not want to use locales just put up this information in flash[:notice] itself.

More dynamic ?

Write a function and use the same flash [:notice] there. Wont hurt at all.

want more data ?

Heres a quick solution, i always <%= params%> in my views to know easily whats going and whats coming. You can then open your rails console and play along with different actions and so on.

user = User.new
user.save
user.errors.messages

All of this is good enough data, i think.

Good luck.

相关问答

更多

相关文章

更多

最新问答

更多
  • 带有简单redis应用程序的Node.js抛出“未处理的错误”(Node.js with simple redis application throwing 'unhandled error')
  • 高考完可以去做些什么?注意什么?
  • Allauth不会保存其他字段(Allauth will not save additional fields)
  • Flask中的自定义中止映射/异常(Custom abort mapping/exceptions in Flask)
  • sed没有按预期工作,从字符串中间删除特殊字符(sed not working as expected, removing special character from middle of string)
  • 怎么在《我的世界》游戏里面编程
  • .NET可移植可执行文件VS .NET程序集(.NET Portable Executable File VS .NET Assembly)
  • 搜索字符串从视图中键入两个字段的“名字”和“姓氏”组合(Search Strings Typed from View for Two Fields 'First Name' and 'Last Name' Combined)
  • 我可以通过配置切换.Net缓存提供程序(Can I switch out .Net cache provider through configuration)
  • 在鼠标悬停或调整浏览器大小之前,内容不会加载(Content Does Not Load Until Mouse Hover or Resizing Browser)
  • 未捕获的TypeError:auth.get不是函数(Uncaught TypeError: auth.get is not a function)
  • 如何使用变量值创建参数类(How to create a parameter class with variant value)
  • 在std :: deque上并行化std :: replace(Parallelizing std::replace on std::deque)
  • 单元测试返回Connection对象的方法(Unit Test for a method that returns a Connection object)
  • rails:上传图片时ios中的服务器内部错误(rails: server internal error in ios while uploading image)
  • 如何在Android中构建应用程序警报[关闭](How build an application Alarm in Android [closed])
  • 以编程方式连接到Windows Mobile上的蓝牙耳机(Programmatically connect to bluetooth headsets on Windows Mobile)
  • 在两个不同的SharedPreference中编写并获得相同的结果(Writing in two different SharedPreference and getting the same result)
  • CSS修复容器和溢出元素(CSS Fix container and overflow elements)
  • 在'x','y','z'迭代上追加数组(Append array on 'x', 'y', 'z' iteration)
  • 我在哪里可以看到使用c ++源代码的UML方案示例[关闭](Where I can see examples of UML schemes with c++ source [closed])
  • SQL多个连接在与where子句相同的表上(SQL Multiple Joins on same table with where clause)
  • 位字段并集的大小,其成员数多于其大小(Size of bit-field union which has more members than its size)
  • 我安装了熊猫,但它不起作用(I installed pandas but it is not working)
  • Composer - 更改它在env中使用的PHP版本(Composer - Changing the version of PHP it uses in the env)
  • 使用JavaFX和Event获取鼠标位置(Getting a mouse position with JavaFX and Event)
  • 函数调用可以重新排序(Can function calls be reordered)
  • 关于“一对多”关系的NoSQL数据建模(NoSQL Data Modeling about “one to many” relationships)
  • 如何解释SBT错误消息(How to interpret SBT error messages)
  • 调试模式下的Sqlite编译器错误“初始化程序不是常量”(Sqlite compiler errors in Debug mode “initializer is not a constant”)