dongyakui8675 2018-03-27 12:15
浏览 32
已采纳

冗余if语句

I'm wondering if I can speed my process up, and this is the only area where I'm thinking it might be taking longer than necessary.

Basically, I'm running different select statements in db2 and mysql. I load those results in arrays to prepare them for comparison. Then I'm taking the count arrays and using them as conditions in my if statements.

The problem is I have 3 sections of IF/ELSE and the first 2 do the same exact thing (performing the same exact insert statement, just based on 2 different conditions).

$count3 holds records that exist in the table and have expired
$count4 holds records that exist in the table and have not expired

So if they're both empty, that means records don't exist so I insert. If $count3 is not empty, that means those records have expired so I insert also. If $count4 is not empty, that means records haven't expired so I update. I'm wondering if the redundancy if these first two IF/ELSE blocks are making it take longer than it could.

Here's the code:

if(empty($count3) && empty($count4)){

    //execute query 1
}
elseif(!empty($count3)){

    //execute query 1
}
elseif(!empty($count4)){

    //execute query 2
}

Is there a better way to say something like:

if (count3 and count4 are BOTH empty) OR if(count3 is not empty){
     insert
}elseif(count 4 is not empty){
    updated
}
  • 写回答

4条回答 默认 最新

  • douxin2002 2018-03-27 12:19
    关注

    Truth table:

    c3      c4      query
    -------------------
    empty   empty   1
    !empty  empty   1
    empty   !empty  2
    !empty  !empty  1   
    

    So, simplified:

    if (empty($count3) && !empty($count4)) {
        // query 2
    } else {
        // query 1
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序