考虑使用以下属性进行表名测试
+-------+------------+---------------------------+----------+
| 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 |
+-------+------------+---------------------------+----------+
如同在此表中每次进入表中的新插入时,它必须检查新覆盖值是否大于现有的最小覆盖值,如果为真,我需要更新具有现有最小覆盖值的新条目,以便我可以只保留表中最高的3个覆盖率值。
考虑一个新的条目覆盖= 29因为它大于min(coverage)= 28然后我需要更新覆盖值29和测试用例名称。
使用python-mysql我这样做请让我知道python mysql查询/代码来执行以下操作。
我使用python
从列表中向表中插入值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()
所以无论何时出现新值,都必须检查min(coverage)值。如果新值大于它必须更新的现有min(coverage),否则它需要忽略该循环
答案 0 :(得分:0)
我认为这对于插件来说太复杂了。在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)