En mulighed her ville være at lave en selvtilslutning med payment
tabel, slutter sig til customer_id
og order_id
kolonner. Du kan tilføje endnu et tilmeldingsfunktionsopkald og derefter bruge resultatet i dit valg. Følgende løsning bruger en rå forespørgsel, da Codeigniter ikke ser ud til at tolerere aritmetik i forbindelsestilstanden:
public function order_balance($order_code)
{
$this->db->query("SELECT p1.*, p2.balance AS previous_balance FROM payment p1 INNER JOIN payment p2 ON p1.order_id = p2.order_id + 1 AND p1.customer_id = p2.customer_id LEFT JOIN services s ON p1.customer_id = s.customer_id ORDER BY p1.id DESC");
$query = $this->db->get_where('p1', array('code' => $order_code));
return $query->previous_row();
}
Forespørgslen har formen:
SELECT p1.*, p2.balance AS previous_balance
FROM payment p1 INNER JOIN payment p2
ON p1.order_id = p2.order_id + 1 AND
p1.customer_id = p2.customer_id
LEFT JOIN services s
ON p1.customer_id = s.customer_id
ORDER BY p1.id DESC