mysql 查询当某一条件满足时 的那一条数据
程序员文章站
2022-07-13 14:11:06
...
查询当某一条件满足时的那一条数据
首先准备一个测试的表以及数据
-- 每日代码行数记录表
create table code_line_record(
id_ int primary key auto_increment,
name varchar(20),
create_time_ date,
line_number_ int
);
insert into code_line_record values(null, "qiaolin", "2018-03-01", 100);
insert into code_line_record values(null, "qiaolin", "2018-03-02", 200);
insert into code_line_record values(null, "qiaolin", "2018-03-03", 300);
insert into code_line_record values(null, "qiaolin", "2018-03-04", 400);
insert into code_line_record values(null, "qiaolin", "2018-03-05", 500);
insert into code_line_record values(null, "qiaolin", "2018-03-06", 600);
insert into code_line_record values(null, "qiaolin", "2018-03-07", 700);
insert into code_line_record values(null, "qiaolin", "2018-03-08", 800);
insert into code_line_record values(null, "qiaolin", "2018-03-09", 900);
insert into code_line_record values(null, "qiaolin", "2018-03-10", 1000);
我这里是查询代码写到1000行的是哪一天,反正给大家的是个案例,实际的需求看自己的运用!
代码如下:
select * from (
select clr.*,
if(@sum + line_number_ >= 1000 and @isOk = 'no',
@isOk := 'yes', @sum := @sum + line_number_) result,
@sum , @isOk
from code_line_record clr , (SELECT @sum := 0, @isOk := 'no') AS it
) temp where temp.result = 'yes'