I'm using Codeigniter 2 ActiveRecord to execute a query. In my table I have two fields reduced_price
(which can be empty) and price
. I would like the Order By
to check reduced_price
first, and if it is empty it use price
.
My current code looks like this :
$this->db->select('*')
->from('products')
->order_by('reduced_price', 'desc')
->get()
->result();
How can I modify my code to achieve this?
Use case in query $sql = 'SELECT * FROM products ORDER_BY CASE WHEN reduced_price IS NOT NULL THEN reduced_price ELSE price END'
$query= $this->db->query($sql); return $query->result_array();
use case like this is sql query
ORDER BY CASE WHEN reduced_price IS NOT NULL THEN reduced_price END, CASE WHEN reduced_price IS NULL THEN price END