当前位置: 首页 > 面试题库 >

SQL-在MySQL中遍历表的每一行?

吉嘉珍
2023-03-14
问题内容

所以我有2张桌子communication,和movement

communication具有列fromIDtimestamp具有呼叫者的ID和进行呼叫的时间。然后,我有另一台movementIDtimestampxy,,有一个人,他们的位置的ID
(x,y),而且它们在该位置的时间。

我想写一个看起来像这样的查询:

For every single row of communication(R)
    SELECT * FROM movement m
    WHERE m.ID = R.fromID && m.timestamp <= R.timestamp
    ORDER BY timestamp

基本上,这是在寻找movement timestamp给定值的最接近值communication timestamp。之后,最终,我想根据数据查找(x,y)呼叫的位置movement

我该怎么做?我知道有一种基于集合的方法,但是我不想那样做。我调查了一下cursors,但是我感到在那方面的表现很糟糕……所以循环中是否有这样做的方法呢?我本质上是想遍历的每一行communication并获取结果…

我尝试过这样的事情:

DELMITER $$ 
CREATE PROCEDURE findClosestTimestamp() 
BEGIN 
DECLARE commRowCount DEFAULT 0; 
DECLARE i DEFAULT 0; 
DECLARE ctimestamp DEFAULT 0; 
SELECT COUNT(*) FROM communication INTO commRowCount;

SET i = 0; 
WHILE i < commRowCount DO 
SELECT timestamp INTO ctimestamp FROM communication c 
SELECT * FROM movement m 
WHERE m.vID = c.fromID && m.timestamp <= R.timestamp
END$$ 
DELIMITER ;

但是我知道那是完全错误的……是做这种游标的唯一方法吗?我只是在Internet上的任何地方都找不到这样的示例,而且我对SQL过程完全陌生。任何指导将不胜感激,谢谢!!


问题答案:

让我们看看我是否可以使用光标将您指向正确的方向:

delimiter $$
create procedure findClosestTimeStamp()
begin
    -- Variables to hold values from the communications table
    declare cFromId int;
    declare cTimeStamp datetime;
    -- Variables related to cursor:
    --    1. 'done' will be used to check if all the rows in the cursor 
    --       have been read
    --    2. 'curComm' will be the cursor: it will fetch each row
    --    3. The 'continue' handler will update the 'done' variable
    declare done int default false;
    declare curComm cursor for
        select fromId, timestamp from communication; -- This is the query used by the cursor.
    declare continue handler for not found -- This handler will be executed if no row is found in the cursor (for example, if all rows have been read).
        set done = true;

    -- Open the cursor: This will put the cursor on the first row of its
    -- rowset.
    open curComm;
    -- Begin the loop (that 'loop_comm' is a label for the loop)
    loop_comm: loop
        -- When you fetch a row from the cursor, the data from the current
        -- row is read into the variables, and the cursor advances to the
        -- next row. If there's no next row, the 'continue handler for not found'
        -- will set the 'done' variable to 'TRUE'
        fetch curComm into cFromId, cTimeStamp;
        -- Exit the loop if you're done
        if done then
            leave loop_comm;
        end if;
        -- Execute your desired query.
        -- As an example, I'm putting a SELECT statement, but it may be
        -- anything.
        select *
        from movement as m
        where m.vID = cFromId and m.timeStamp <= cTimeStamp
        order by timestampdiff(SECOND, cTimeStamp, m.timeStamp)
        limit 1;
    end loop;
    -- Don't forget to close the cursor when you finish
    close curComm;
end $$
delimiter ;

参考:

  • MySQL参考:游标
  • MySQL参考:日期和时间函数- timestampdiff()


 类似资料:
  • 问题内容: 我想从包含一列的每个表中输出数据。我将以下过程放在一起,但是在我的循环中,mysql从字面上解释表名,而不是评估变量。解决办法是什么? 问题答案: 尝试这个:

  • 问题内容: 我正在运行以下代码,从具有特定列的所有表中提取所有相关的行。外层应该检查该迭代表中是否存在该列。如果没有,它应该完成该迭代并移至下一张表。如果表中有该列,则应检查该表是否将返回任何记录。如果没有要返回的记录,则应结束该迭代并移至下一个表。如果有记录,则应在SSMS中显示它们。 这似乎可行,因为SSMS仅返回带有有效条目的网格。我不明白的是:为什么我仍然会收到这些错误? 编辑 使用建议后

  • 问题内容: 我有一个允许用户上载文本文件或将文件内容复制/粘贴到文本区域的表格。我可以轻松地区分两者,然后将它们输入的任何一个放入字符串变量中,但是我从那里去哪里呢? 我需要遍历字符串的每一行(最好不要担心不同机器上的换行符),确保它只有一个令牌(没有空格,制表符,逗号等),清理数据,然后生成一个SQL查询基于所有方面。 我是一个非常优秀的程序员,所以我知道如何做的一般想法,但是自从使用PHP以来

  • 问题内容: 尝试概括我的问题…我想为SELECT语句返回的每个结果执行一个存储过程。 精神上,我想尝试类似EXEC myStoredProc(从某个表的SELECT ID中选择cond = @param) 与我的特定案例有关的更多详细信息…我有一个SaaS应用程序。我想从系统中删除一个租户。在删除租户之前,我必须删除与该租户关联的数据库中的所有记录。 租户拥有诸如包含许多不同类型字段的表单之类的项

  • 问题内容: 我想遍历输出的每一行: 现在我正在尝试: 但是,这会分别遍历行中的每个元素,因此我得到: 但是,我想遍历整个行。我怎么做? 问题答案: 将IFS设置为换行符,如下所示: 如果您不想永久设置IFS,请在其周围放一个子外壳: 或同时使用| 改为阅读: 还有一个选项,它在同一shell级别上运行while / read:

  • 问题内容: 考虑表SAMPLE: 有一个称为的存储过程。它只需要一个参数(即id) 给定名称作为参数,找到带有的所有行并将所有这些ID传递给 例如: 当被传递,被传递到个别。 即应发生以下情况: 我无法触摸也看不到它的内容。我只需要将值传递给它。 问题答案: 如果 必须 进行迭代(*),请使用旨在进行迭代的结构-cursor。我非常讨厌,但是如果它最清楚地表达了您的意图,请说: (*)这个答案最近