這應爲一個SQL解決工作排序。雖然它只會給你一個week_id的catch_id問題。我不知道你的表被稱爲所以我在下面的答案稱之爲consecutive
:
drop table if exists consecutive;
create table consecutive
(id int,week_id int,user_id int,catch_id int);
insert into consecutive values (1,2,6,0);
insert into consecutive values (1,3,6,1);
insert into consecutive values (1,4,6,1);
insert into consecutive values (1,5,6,1);
insert into consecutive values (1,6,6,0);
insert into consecutive values (1,7,6,0);
insert into consecutive values (1,8,6,2);
insert into consecutive values (1,9,6,0);
insert into consecutive values (1,10,6,0);
insert into consecutive values (1,11,6,1);
select w,count(*) as max_consecutive_weeks
from
(
select
case when @cur_catch_id != catch_id then @cur_week_id := week_id else @cur_week_id end as w,
@cur_catch_id := catch_id as catchChange,
c.*
from consecutive c
cross join (select @cur_catch_id := -1,@cur_week_id := -1) t
where user_id = 6
order by week_id asc
) t
where catch_id = 1
group by w
order by max_consecutive_weeks desc,w asc
limit 1;
您可以使用相同的查詢,通過改變where catch_id = 1
到where catch_id = 0
以獲得最大的連續week_ids與catch_id = 0。
祝你好運!
查詢不能正常工作。只是運行它,並看到 – aWebDeveloper
道歉。在別名中輸入錯字。我編輯了我的答案。 –