Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

custom query, sum(), avg()

edited March 2014 in Framework

I would like to fetch the sum (or avg) of a column.

Is it possible?

Also I'm interested in adding subqueries but I have not been able to find examples in the documentation.



  • You can fetch a sum or avg like this:

    $res = User::query ('sum(id) as count, avg(id) as average')->fetch_orig ();
    echo $res->count;
    echo $res->average;

    The query builder doesn't do subqueries aside from many-many relations. I usually write subqueries and complex joins manually through DB::fetch() so I have more control and can test for optimal performance.

  • Prodigious! ;)

    Thank you.

Sign In or Register to comment.