php语句到sql case语句

bvhaajcl  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(265)

关闭。这个问题需要更加突出重点。它目前不接受答案。
**想改进这个问题吗?**通过编辑这篇文章更新这个问题,使它只关注一个问题。

11个月前关门了。
改进这个问题
有人能帮我解释一下这个代码吗。它是一个程序员给我的,我不知道如何在sql查询中使用它。我想用这些条件在sql查询中创建case语句。非常感谢您的帮助!

for($i=0;$i<$dataCount;$i++) {
        $row = $data->data[$i];
        if($row->crefRefeeSignupId == null) {                       //so if the signup ID is null
            $return->data["open"]++;                                //the referral is open
        } else if($row->installScheduled == null) {                 //if the scheduled time is null
            $return->data["pending"]++;                             //it's pending an install
        } else if($row->installTicketResultId == null) {            //if the resolution is null
            $return->data["pending"]++;                             //it's also pending an install
        } else if(in_array($row->installTicketResultId, array(1, 2, 3, 46, 47, 48))) {  //if the ticket result ID is one of the 'success' ones
            $ninetyComplete = ($row->installScheduled + 2592000);   //how far out for the 90 days
            if(time() < $ninetyComplete) {                          //if we're not at 90-days complete
                $return->data["term"]++;                            //they're in their term
            } else if(in_array($row->crefReferCreditId,array(0,null)) || in_array($row->crefRefeeCreditId,array(0,null))) {
                $return->data["unCredited"]++;
            } else if(!in_array($row->crefReferCreditId,array(0,null)) || !in_array($row->crefRefeeCreditId,array(0,null))) {
                $return->data["credited"]++;
            } else {
                $return->data["pending"]++;
            }
        } else {                                                    //otherwise
            $return->data["cancelled"]++;                           //it's cancelled
        }
    }
jutyujz0

jutyujz01#

您看到的是一个循环,它聚合了不同类型的注册。它将不同注册状态的计数累积到一个数组中,如下所示:

$return->data = [
    'open' => 0,
    'pending' => 0,
    'term' => 0,
    'unCredited' => 0,
    'credited' => 0,
    'cancelled' => 0
];

以下是对表列中的每一行计算的条件,以及结果状态:

if "crefRefeeSignupId" is NULL: open
else if "installScheduled" is NULL: pending
else if "installTicketResultId" is NULL: pending

else if "installTicketResultId" is IN (1, 2, 3, 46, 47, 48), then:
    if "installScheduled" < 90 days: term
    else if "crefReferCreditId" or "crefRefeeCreditId" is IN (0, NULL): unCredited
    else if "crefReferCreditId" or "crefRefeeCreditId" is NOT IN (0, NULL): credited
    else: pending

else: cancelled

上面的“翻译”应该足够清楚,以帮助您设计您可能需要的任何sql语句。我不确定建立一个怪物案例陈述是正确的方法。您可能需要更好地为自己定义从数据集中实际需要的信息。

相关问题