光标是执行此操作的唯一方法吗?

发布于 2024-10-31 16:12:00 字数 483 浏览 3 评论 0原文

我正在翻阅我拥有的一些原始数据。数据存储在 MySQL 数据库中。数据以逐毫秒的格式列出了当前正在发生的许多可能的“事件”中的哪些。它只有几列:

  1. id - 行
  2. 事件的唯一标识符 - 指示当前正在发生哪个事件

我想做的是获取有关这些数据的一些基本信息。具体来说,我想创建一个表,其中包含:

  1. 事件开始的 id
  2. 事件结束的 id
  3. 索引事件及其发生的新 id,以及详细说明当前发生的事件的列。

我知道使用 PHP 很容易处理,只需使用一个简单的循环遍历所有记录,但我试图在这里突破我的 MySQL 知识的界限(这可能很危险,我知道!! )。

所以,我的问题是:光标是最好的选择吗?我问,因为事件可能会发生多次,所以按事件类型进行分组之类的操作将不起作用 - 或者会吗?我只是想知道是否有一种聪明的方法来处理我错过的这个问题,而无需按顺序遍历每一行。

谢谢!

I'm in the process of churning through some raw data that I have. The data are in a MySQL database. The data lists, in a millisecond-by-millisecond format, which of a number of possible 'events' are currently happening. It has only a few columns:

  1. id - unique identifier for the row
  2. event - indicates which event is currently occurring

What I would like to do is get some basic information regarding these data. Specifically, I'd like to create a table that has:

  1. The id that an event starts
  2. The id that an event ends
  3. A new id indexing the events and their occurrence, as well as a column detailing which event is currently happening.

I know that this would be easy to deal with using PHP, just using a simple loop through all the records, but I'm trying to push the boundaries of my MySQL knowledge for a bit here (it may be dangerous, I know!!).

So, my question is this: would a cursor be the best thing to use for this? I ask because events can occur multiple times, so doing something like grouping by the event type won't work - or will it? I'm just wondering if there is a clever way of dealing with this I have missed, without needing to go through each row sequentially.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我还不会笑 2024-11-07 16:12:00

为了演示我之前评论的内容,假设您有下表:

event_log
  id    INT NOT NULL AUTO_INCREMENT PRIMARY KEY
  start DATETIME
  event VARCHAR(255)                     # or whatever you want for datatype

收集此信息非常简单:

SELECT   el.*,
         (SELECT   el_j.start            # -
          FROM     event_log el_j        #  |
          WHERE    el_j.id > el.id       #  |- Grab next row based on the next ID
          LIMIT    1) as end             # -
FROM     event_log
ORDER BY start;

To demonstrate what I commented earlier about, say you have the following table:

event_log
  id    INT NOT NULL AUTO_INCREMENT PRIMARY KEY
  start DATETIME
  event VARCHAR(255)                     # or whatever you want for datatype

Gathering this information is as simple as:

SELECT   el.*,
         (SELECT   el_j.start            # -
          FROM     event_log el_j        #  |
          WHERE    el_j.id > el.id       #  |- Grab next row based on the next ID
          LIMIT    1) as end             # -
FROM     event_log
ORDER BY start;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文