MySQL-返回在匹配条件之后有n个连续记录的所有记录

MySQL-返回在匹配条件之后有n个连续记录的所有记录,mysql,Mysql,MySQL v5.5 Hi-我需要从一个简单的表中创建一个select查询,在该表中,它返回所有在后面有一定数量的连续行且标记为可用的行 示例表数据如下所示: id Slot Available 0 5.00pm 1 1 5.10pm 1 2 5.20pm 1 3 5.30pm 0 4 5.40pm 1 5 5.50pm 1 6 6.00pm 1 7 6.10pm 1 我有一个变量,它将决定所需插槽的数量 例如,如

MySQL v5.5

Hi-我需要从一个简单的表中创建一个select查询,在该表中,它返回所有在后面有一定数量的连续行且标记为可用的行

示例表数据如下所示:

id   Slot    Available
0    5.00pm  1
1    5.10pm  1   
2    5.20pm  1
3    5.30pm  0
4    5.40pm  1
5    5.50pm  1
6    6.00pm  1
7    6.10pm  1
我有一个变量,它将决定所需插槽的数量

例如,如果变量为3,我需要结果返回所有有2个连续可用插槽的插槽

本例中的预期结果为:

id   Slot    Available
0    5.00pm  1
4    5.40pm  1
5    5.50pm  1
任何援助都将不胜感激

(我不是专业的开发者,所以请温柔一点:))


非常感谢

我相信有一种方法可以用SQL实现您想要的功能。我有一个您可以使用的存储过程示例

DELIMITER //

DROP PROCEDURE IF EXISTS GetTimeSlots;

CREATE PROCEDURE GetTimeSlots(NoOfSlots int)
BEGIN

    declare finished bool default false;

    -- stores id, slot and available from the cursor
    declare v_id int;
    declare v_slot text;
    declare v_available int;

    -- stores how many slots were found consecutively
    declare v_slotsfound int;

    -- loop through all available records
    declare c cursor for
    select id, slot, available from test where available = 1;

    declare continue handler for not found
    set finished = true;

    -- create empty table to store slots we find
    drop temporary table if exists tmp;
    create temporary table tmp
    select * from test where 1=2;

    open c;
    looper: loop
        fetch c into v_id, v_slot, v_available;
        if finished then
            leave looper;
        end if;

        select sum(case
                   when available then 1
                   else 0
                   end) into v_slotsfound
        from (
            -- list out as many slots you need
            -- outer query will total up the # of available slots
            select available from test where id >= v_id
            order by id
            limit NoOfSlots
        ) t;

        -- if # of available slots equals how many you want, store data
        -- in temporary table
        if v_slotsfound = NoOfSlots then
            insert into tmp values (v_id, v_slot, v_available);
        end if;

    end loop looper;
    close c;

    -- output the results
    select * from tmp;
    drop temporary table if exists tmp;

END //

DELIMITER ;
运行它 如果需要两个连续插槽:

mysql> call gettimeslots(2);
+------+--------+-----------+
| id   | slot   | available |
+------+--------+-----------+
|    0 | 5.00pm |         1 |
|    1 | 5.10pm |         1 |
|    4 | 5.40pm |         1 |
|    5 | 5.50pm |         1 |
|    6 | 6.00pm |         1 |
+------+--------+-----------+
mysql> call gettimeslots(4);
+------+--------+-----------+
| id   | slot   | available |
+------+--------+-----------+
|    4 | 5.40pm |         1 |
+------+--------+-----------+
如果您想要4个连续插槽:

mysql> call gettimeslots(2);
+------+--------+-----------+
| id   | slot   | available |
+------+--------+-----------+
|    0 | 5.00pm |         1 |
|    1 | 5.10pm |         1 |
|    4 | 5.40pm |         1 |
|    5 | 5.50pm |         1 |
|    6 | 6.00pm |         1 |
+------+--------+-----------+
mysql> call gettimeslots(4);
+------+--------+-----------+
| id   | slot   | available |
+------+--------+-----------+
|    4 | 5.40pm |         1 |
+------+--------+-----------+
使用的数据
我相信有一种方法可以实现您对SQL的需求。我有一个您可以使用的存储过程示例

DELIMITER //

DROP PROCEDURE IF EXISTS GetTimeSlots;

CREATE PROCEDURE GetTimeSlots(NoOfSlots int)
BEGIN

    declare finished bool default false;

    -- stores id, slot and available from the cursor
    declare v_id int;
    declare v_slot text;
    declare v_available int;

    -- stores how many slots were found consecutively
    declare v_slotsfound int;

    -- loop through all available records
    declare c cursor for
    select id, slot, available from test where available = 1;

    declare continue handler for not found
    set finished = true;

    -- create empty table to store slots we find
    drop temporary table if exists tmp;
    create temporary table tmp
    select * from test where 1=2;

    open c;
    looper: loop
        fetch c into v_id, v_slot, v_available;
        if finished then
            leave looper;
        end if;

        select sum(case
                   when available then 1
                   else 0
                   end) into v_slotsfound
        from (
            -- list out as many slots you need
            -- outer query will total up the # of available slots
            select available from test where id >= v_id
            order by id
            limit NoOfSlots
        ) t;

        -- if # of available slots equals how many you want, store data
        -- in temporary table
        if v_slotsfound = NoOfSlots then
            insert into tmp values (v_id, v_slot, v_available);
        end if;

    end loop looper;
    close c;

    -- output the results
    select * from tmp;
    drop temporary table if exists tmp;

END //

DELIMITER ;
运行它 如果需要两个连续插槽:

mysql> call gettimeslots(2);
+------+--------+-----------+
| id   | slot   | available |
+------+--------+-----------+
|    0 | 5.00pm |         1 |
|    1 | 5.10pm |         1 |
|    4 | 5.40pm |         1 |
|    5 | 5.50pm |         1 |
|    6 | 6.00pm |         1 |
+------+--------+-----------+
mysql> call gettimeslots(4);
+------+--------+-----------+
| id   | slot   | available |
+------+--------+-----------+
|    4 | 5.40pm |         1 |
+------+--------+-----------+
如果您想要4个连续插槽:

mysql> call gettimeslots(2);
+------+--------+-----------+
| id   | slot   | available |
+------+--------+-----------+
|    0 | 5.00pm |         1 |
|    1 | 5.10pm |         1 |
|    4 | 5.40pm |         1 |
|    5 | 5.50pm |         1 |
|    6 | 6.00pm |         1 |
+------+--------+-----------+
mysql> call gettimeslots(4);
+------+--------+-----------+
| id   | slot   | available |
+------+--------+-----------+
|    4 | 5.40pm |         1 |
+------+--------+-----------+
使用的数据
你能澄清或提供更多的例子吗?1) “连续两个插槽可用”是什么意思?我看到集合中的所有插槽都是连续的,并且排序顺序中有提供。2) 为什么正好是两个连续的插槽,什么是“连续”呢?3) 为什么在所需的解决方案中返回0,4,5而不是0,1,7?区别是什么?你能澄清或提供更多的例子吗?1) “连续两个插槽可用”是什么意思?我看到集合中的所有插槽都是连续的,并且排序顺序中有提供。2) 为什么正好是两个连续的插槽,什么是“连续”呢?3) 为什么在所需的解决方案中返回0,4,5而不是0,1,7?有什么区别吗?太棒了,谢谢!最后一件事,因为我完全不知道它是如何工作的,我只需要始终包括第一个插槽,而不考虑连续的插槽。请你给我指一下正确的方向好吗。再次感谢你,你能举例说明@user1830274是什么意思吗?您可以编辑问题并添加示例。告诉我你想要什么。你提供的代码工作得很好,我只需要始终返回第一行(id 0),只要它标记为可用。哦,我明白了。就在上面
select*from tmp行,类型<代码>插入tmp中,从测试中选择*如果可用=1限值1将在所有其他行上方添加第一个可用的时间段。为了更好地测量,您还应该更改
select*fromtmp
按id从tmp订单中选择*
谢谢,真的非常感谢,谢谢!最后一件事,因为我完全不知道它是如何工作的,我只需要始终包括第一个插槽,而不考虑连续的插槽。请你给我指一下正确的方向好吗。再次感谢你,你能举例说明@user1830274是什么意思吗?您可以编辑问题并添加示例。告诉我你想要什么。你提供的代码工作得很好,我只需要始终返回第一行(id 0),只要它标记为可用。哦,我明白了。就在上面
select*from tmp行,类型<代码>插入tmp中,从测试中选择*如果可用=1限值1将在所有其他行上方添加第一个可用的时间段。为了更好地测量,您还应该更改
select*fromtmp
按id从tmp订单中选择*
谢谢,非常感谢