My case is like this :
case when is like this :
public function get_opportunity($customer_id, $group_id, $action)
{
$sql = "SELECT COUNT(transaction_id) AS total_transaction
FROM `transaction`
WHERE
CASE
WHEN '$action' = 'view_all' THEN transaction_id IS NOT NULL
WHEN '$action' = 'view_group' THEN group_id IN($group_id)
ELSE transaction_created_by = ?
END
";
$result = $this->db->query($sql, array($customer_id))->result_array();
return ($result[0]['total_transaction']) ? $result[0]['total_transaction'] : 0;
}
if else is like this :
public function get_opportunity($customer_id, $group_id, $action)
{
if($action == "view_all")
$condition = " transaction_id IS NOT NULL";
else if($action == 'view_group')
$condition = " group_id IN($group_id)";
else
$condition = " customer_id = ?";
$sql = "SELECT COUNT(transaction_id) AS total_transaction
FROM `transaction`
WHERE $condition";
$result = $this->db->query($sql, array($customer_id))->result_array();
return ($result[0]['total_transaction']) ? $result[0]['total_transaction'] : 0;
}
I have tried it. The all type of the above work. But here I ask, which one is better?