【问题标题】:update a table in mysql if the current value is greater than existing minimum value if not ignore that update如果当前值大于现有最小值,则更新 mysql 中的表,如果不忽略该更新
【发布时间】:2018-04-12 08:57:57
【问题描述】:

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

+-------+------------+---------------------------+----------+
| 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 个值。

考虑到一个新条目带有coverage= 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),它必须更新,否则它需要忽略该循环

【问题讨论】:

  • 您要求提供代码补全服务。阅读您的问题,我知道您知道该怎么做:“如果这是真的,它必须检查新的覆盖率值是否大于现有的最小覆盖率值”,并且此要求不在您的代码中。只需编写它,如果您发现从数据库中读取值的一些问题,请点击此处Minimal, Complete, and Verifiable example
  • 如果表中没有任何条目(或 1 或 2)会怎样?
  • 我会考虑一个mysql过程而不是一个插入语句。
  • @P.Salmon 如果我们没有条目,那么它必须作为新行插入
  • @danihp 代码是:query("update testing set coverage="%s" ,test_case="%s" where file_name like"%s" and coverage=(select min(coverage) from testing其中 file_name like "%s")"),(x,y,z,z)

标签: python mysql database python-2.7 mysql-python


【解决方案1】:

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

【讨论】:

  • 但结果它在第二行显示 27 而不是 28
  • 如果您有一条规则说更新的值必须大于最小值,那么您需要在更新之前对此进行测试。
  • 我收到错误,因为 ERROR 1241:操作数应包含 1 列
  • 已编辑以包含 > 分钟的测试。顺便说一句,您可能希望在插入时包含更新测试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-17
  • 1970-01-01
  • 2012-11-22
  • 1970-01-01
  • 2020-01-01
  • 2018-03-19
  • 1970-01-01
相关资源
最近更新 更多