如果当前值大于现有最小值,则在 mysql 中更新 table 如果不忽略该更新
update a table in mysql if the current value is greater than existing minimum value if not ignore that update
考虑使用以下属性进行 table 名称测试
+-------+------------+---------------------------+----------+
| id | test_case | file_name | coverage |
+-------+------------+---------------------------+----------+
| 8645 | test case1 | /vendor/src/kmod/vendor.c | 32 |
| 12456 | test case4 | /vendor/src/kmod/vendor.c | 28 |
| 20258 | test case3 | /vendor/src/kmod/vendor.c | 30 |
+-------+------------+---------------------------+----------+
就像这个table一样,当每个新插入到table时,它必须检查新的覆盖值是否大于现有的最小覆盖值,如果是的话,我需要更新新的具有现有最小覆盖值的条目,以便我只能保留 table.
中的前 3 个最高覆盖值
考虑到新条目的覆盖率 = 29,因为它大于 min(coverage)=28 那么我需要用 29 和测试用例名称更新覆盖率值。
使用 python-mysql 我正在这样做,请让我知道 python mysql query/code 按照上面的步骤执行此操作。
我正在使用 python
从列表中向 table 插入值
for x,y in zip(out2,out4):
cur.execute("insert into testing(test_case,file_name,coverage) values('test case8',%s,%s) on duplicate file_name like "%s" update coverage=case when values(coverage) then values(coverage) else coverage end",(x,y,x))
db.commit()
因此,无论何时出现新值,它都必须检查最小(覆盖)值。如果新值大于现有的最小(覆盖),则必须更新,否则它需要忽略该循环
我认为这对于插入处理来说太复杂了。在 mysql 中,我会使用一个过程,但您可能想在前端执行此操作。请注意,如果某些事件发生,我不确定你想要什么。
drop table if exists t;
create table t( id int , test_case varchar(20), file_name varchar(30), coverage int);
drop procedure if exists t;
delimiter $$
create procedure t(inid int , intest_case varchar(20), infile_name varchar(30), incoverage int)
begin
declare cnt int;
declare minid int;
declare minvoverage int;
declare covexists int;
set cnt = (select count(*) from t where file_name = infile_name);
set @mincoverage = (select min(coverage) from t where file_name = infile_name);
set minid = (select min(id) from t where file_name = infile_name and coverage = @mincoverage);
if cnt < 3 then
insert into t values (inid,intest_case,infile_name, incoverage);
else
if @mincoverage <> incoverage then #retain existsing detail
if not exists(select 1 from t where file_name = infile_name and coverage = incoverage) then #retain existing detail
update t
set test_case = intest_case, coverage = incoverage
where file_name = infile_name and
id = minid;
end if;
end if;
end if;
end$$
delimiter ;
call t(8645 , 'test case1' , '/vendor/src/kmod/vendor.c' , 32 );
call t(12456, 'test case2' , '/vendor/src/kmod/vendor.c' , 28 );
call t(20258, 'test case3' , '/vendor/src/kmod/vendor.c' , 30 );
call t(30000, 'test case4' , '/vendor/src/kmod/vendor.c' , 27 );
select * from t;
结果
+-------+------------+---------------------------+----------+
| id | test_case | file_name | coverage |
+-------+------------+---------------------------+----------+
| 8645 | test case1 | /vendor/src/kmod/vendor.c | 32 |
| 12456 | test case4 | /vendor/src/kmod/vendor.c | 27 |
| 20258 | test case3 | /vendor/src/kmod/vendor.c | 30 |
+-------+------------+---------------------------+----------+
3 rows in set (0.00 sec)
考虑使用以下属性进行 table 名称测试
+-------+------------+---------------------------+----------+
| id | test_case | file_name | coverage |
+-------+------------+---------------------------+----------+
| 8645 | test case1 | /vendor/src/kmod/vendor.c | 32 |
| 12456 | test case4 | /vendor/src/kmod/vendor.c | 28 |
| 20258 | test case3 | /vendor/src/kmod/vendor.c | 30 |
+-------+------------+---------------------------+----------+
就像这个table一样,当每个新插入到table时,它必须检查新的覆盖值是否大于现有的最小覆盖值,如果是的话,我需要更新新的具有现有最小覆盖值的条目,以便我只能保留 table.
中的前 3 个最高覆盖值考虑到新条目的覆盖率 = 29,因为它大于 min(coverage)=28 那么我需要用 29 和测试用例名称更新覆盖率值。
使用 python-mysql 我正在这样做,请让我知道 python mysql query/code 按照上面的步骤执行此操作。
我正在使用 python
从列表中向 table 插入值for x,y in zip(out2,out4):
cur.execute("insert into testing(test_case,file_name,coverage) values('test case8',%s,%s) on duplicate file_name like "%s" update coverage=case when values(coverage) then values(coverage) else coverage end",(x,y,x))
db.commit()
因此,无论何时出现新值,它都必须检查最小(覆盖)值。如果新值大于现有的最小(覆盖),则必须更新,否则它需要忽略该循环
我认为这对于插入处理来说太复杂了。在 mysql 中,我会使用一个过程,但您可能想在前端执行此操作。请注意,如果某些事件发生,我不确定你想要什么。
drop table if exists t;
create table t( id int , test_case varchar(20), file_name varchar(30), coverage int);
drop procedure if exists t;
delimiter $$
create procedure t(inid int , intest_case varchar(20), infile_name varchar(30), incoverage int)
begin
declare cnt int;
declare minid int;
declare minvoverage int;
declare covexists int;
set cnt = (select count(*) from t where file_name = infile_name);
set @mincoverage = (select min(coverage) from t where file_name = infile_name);
set minid = (select min(id) from t where file_name = infile_name and coverage = @mincoverage);
if cnt < 3 then
insert into t values (inid,intest_case,infile_name, incoverage);
else
if @mincoverage <> incoverage then #retain existsing detail
if not exists(select 1 from t where file_name = infile_name and coverage = incoverage) then #retain existing detail
update t
set test_case = intest_case, coverage = incoverage
where file_name = infile_name and
id = minid;
end if;
end if;
end if;
end$$
delimiter ;
call t(8645 , 'test case1' , '/vendor/src/kmod/vendor.c' , 32 );
call t(12456, 'test case2' , '/vendor/src/kmod/vendor.c' , 28 );
call t(20258, 'test case3' , '/vendor/src/kmod/vendor.c' , 30 );
call t(30000, 'test case4' , '/vendor/src/kmod/vendor.c' , 27 );
select * from t;
结果
+-------+------------+---------------------------+----------+
| id | test_case | file_name | coverage |
+-------+------------+---------------------------+----------+
| 8645 | test case1 | /vendor/src/kmod/vendor.c | 32 |
| 12456 | test case4 | /vendor/src/kmod/vendor.c | 27 |
| 20258 | test case3 | /vendor/src/kmod/vendor.c | 30 |
+-------+------------+---------------------------+----------+
3 rows in set (0.00 sec)