python - 如果当前值大于现有最小值,则更新 mysql 中的表,否则忽略该更新

标签 python mysql database python-2.7 mysql-python

考虑使用以下属性测试表名

+-------+------------+---------------------------+----------+
| 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),它必须更新,否则需要忽略该循环

最佳答案

我认为这对于插入处理来说太复杂了。在 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)

关于python - 如果当前值大于现有最小值,则更新 mysql 中的表,否则忽略该更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47031190/

相关文章:

php - 如何获取所有记录,即使是那些没有匹配连接的记录 - MySQL

php - 在 PHP 中循环并更新 sql 表的每一行

python - Django 1.7 应用程序标签不是唯一的,重复 foo

python - 在 Ubuntu 12.04 LTS 上安装 Botched Python(无法导入 MySQL)

python - 到 numpy 数组中非连续元素的距离

php - 更新 MySQL 中的所有记录

ruby-on-rails - Rails 迁移 : Remove column from table

ruby-on-rails - 我应该如何启动这个 Ruby on Rails 应用程序?

mysql - 发布系统的表结构

python - 有没有比 pickle 或常规 Python 文件更快的方法来存储大字典?