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

user1830274:

MySQL v5.5

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

表格数据示例如下:

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

任何帮助将不胜感激。

(我不是专业开发人员,所以请保持谦虚:))

非常感谢

zedfoxus:

我敢肯定,有一种方法可以用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(3);
+------+--------+-----------+
| id   | slot   | available |
+------+--------+-----------+
|    0 | 5.00pm |         1 |
|    4 | 5.40pm |         1 |
|    5 | 5.50pm |         1 |
+------+--------+-----------+

如果要连续2个插槽:

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 |
+------+--------+-----------+

如果要连续4个广告位:

mysql> call gettimeslots(4);
+------+--------+-----------+
| id   | slot   | available |
+------+--------+-----------+
|    4 | 5.40pm |         1 |
+------+--------+-----------+

使用数据

create table test (id int, slot text, avaialable int);
insert into test values
(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);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

MySQL从左表返回所有记录,但右表中有匹配项的记录除外

MySQL返回两个日期之间的所有记录

MySql-当一行与结果匹配时,选择具有相同字段的所有记录

如果至少一行与MySQL中的条件匹配,则检索所有组记录

昨天的所有MySQL记录

MySQL:选择联接表匹配所有值的记录

MySQL-如何选择与所有IN值匹配但在1个或多个表中的记录

PHP Mysql连接具有多个条件和空记录的两个表

MYSQL Union All 不通过两个合并查询返回所有记录

MySQL-有效删除除最后N个以外的所有记录

mysql 对所有记录进行排序,除了具有特定标志的记录

MYSQL-获取具有相同ID的多个记录的所有记录

mysql查询加入,比较两个表并返回第一个表中的所有记录

如何删除在另一个MySQL表中的记录只有在所有符合条件的记录已被标记

MySQL-实时记录所有查询

在mysql中记录所有查询

MySQL删除未选中的所有记录

Mysql join 不显示所有记录

MySQL选择所有记录,并为每个选择单独的记录-在一个查询中

MySQL选择具有匹配值的第一行之后的所有行

还原所有记录,包括与MySQL IN子句重复的记录

选择具有多个记录的正确MySQL记录

mysql-将除1个与多个字段匹配(近重复)的记录之外的所有记录标记为已删除

MySQL仅返回具有n个属性匹配项的项目

当一个记录符合条件时如何标记具有匹配 id 的所有记录

如何获取最新记录或在mysql的列中具有所有不同值的最大对应日期的记录?

MySQL:返回表A的所有行,如果表B中存在记录,则返回true | false

返回与MySQL中列表的2个项目匹配的记录

MySQL 和 Laravel - 检查之前和之后的现有记录