Answer the question
In order to leave comments, you need to log in
Kohana 3.3 and Query Builder - how to select and recalculate rows in one query through the model?
Hello.
Everything is pretty trite, but nevertheless I can’t figure out how to make SELECT and COUNT according to different criteria in one query through the model.
Here is a simple example as already tried:
(select all users whose status = 1)
$query = DB::select('id', 'name', 'status')
->from('users')
->where('status', '=', 1);
$total = clone $query;
$total ->select(array(DB::expr('COUNT(id)'), 'total_users'))->from('users')->execute()->as_array();
$total = DB::select(array(DB::expr('COUNT(`id`)'), 'total_users'))->from('users')->execute()->get('total_users');
Answer the question
In order to leave comments, you need to log in
JOIN or UNION, but the second query must either add the field to all records of the first one, or have the same list of fields. but it's not at all clear why such perversions - if you are afraid of an extra load, then just cache the results.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question