Welcome Guest, Not a member yet? Register   Sign In
Active record order_by the result of multiplying two fields
#6

I like your idea of running the calculation in the select and then ordering on the new named field. It would be a nice clean way to do it. Perhaps you know how I could get this to work with select_min, which when translated into SQL already has the 'as [field_name]' included. This is the value that needs to be multiplied by the rate.

Code:
$this->db->select_min("project_units.price_min")
becomes
Code:
MIN(`project_units`.`price_min`) AS price_min

I it possible to nest active record queries?

Thanks

(11-18-2014, 05:13 PM)bclinton Wrote: I believe you should be able to order by the result of a calculation.  To debug it, log $this->db->last_query() to make sure Active Record is giving you what you want.

Or you could select the calculation,


PHP Code:
$this->db->select('(project_units.price_min * rate) as adjusted_price'); 
and order by the name you give it (adjusted_price in this case)
Reply


Messages In This Thread
RE: Active record order_by the result of multiplying two fields - by andycansdale - 11-18-2014, 05:22 PM



Theme © iAndrew 2016 - Forum software by © MyBB