MySQL 根据特定条件和列值进行分组


mysql group by on certain condition and column value

有4种接收方法。 1)交货 2)取货 3)mobile_topup 4)bank_deposit

如果那里有 2 个移动充值,我只需要一个。但是,如果有 2 种交付方式,那么我需要两种交付方式。为此,我创建了此查询:

$clause = "SELECT id FROM beneficiary_payment_info
           GROUP BY IF(ben_payment_method='mobile_topup', '', ben_payment_method)";

但是,问题是,它也按交付方式分组。我不想这样。现在,我在数据库中有2 mobile_topup2 deliverybank deposit

我需要1 mobile_topup2 delivery1 bank deposit

我只是想知道在这个查询中是否有可能,或者我需要在php中执行此操作?

编辑

暂时的,php我有这个解决方案。但是,它看起来并不可靠。

$mobile_count = array();
for($i=0; $i<count($data); $i++){
    if($data[$i]['ben_payment_method']=='mobile_topup'){
        //unset($data[$i]);
        array_push($mobile_count, $i);
    }
}
if(count($mobile_count) > 1){
    for($i=0; $i<count($mobile_count)-1; $i++){
        unset($data[$mobile_count[$i]]);
    }
}
$clause = "SELECT id FROM beneficiary_payment_info
               GROUP BY IF(ben_payment_method='mobile_topup', ben_payment_method, id,)";