2

I want to do something like this with Codeigniter Wanwizard Datamapper.

SELECT COUNT(amount), SUM(amount) FROM mytable

Basically I want to extract both the total amount and how many row included in a single query. But I couldn't find a way in the documentation page here http://datamapper.wanwizard.eu/ because there's select_sum() for SUM function but no select_count() for COUNT. Is there any way to do it just like in my case? Thank you.

tereško
  • 58,060
  • 25
  • 98
  • 150
Ricky L
  • 23
  • 4
  • 1
    just found it myself...here's the answer :) $object->select_func('COUNT', '*', 'count') $object->select_sum('amount')->get() – Ricky L Aug 10 '12 at 05:49

3 Answers3

0
$this->db->select("COUNT(ratting) as v_count, SUM(ratting) as v_sum");
$this->db->where('user_id', $user_id);
$row = $this->db->get('user')->row();

Output

stdClass Object
(
    [v_count] => 3
    [v_sum] => 7.5
)

Hope will help you.

bhaveshkac
  • 477
  • 2
  • 8
  • 15
0

To transform this

SELECT COUNT(amount), SUM(amount) FROM mytable

in a query in controller with Codeigniter and Datamapper, you can use

$p = new mytablemodel();
$p->select_sum('amount','total')->select_func('COUNT', 'amount', 'count')->get();

and to show, you use

echo $p->total;
echo $p->count;

you can add conditionals too:

$p = new mytablemodel();
$p->select_sum('amount','total')->select_func('COUNT', 'amount', 'count')->where('status', 1)->get();
Leffa
  • 369
  • 4
  • 7
-1

Try this

$p = new Film();
$p->select('COUNT(amount), SUM(amount)')->get();
Navnath Godse
  • 2,233
  • 2
  • 23
  • 32