问题
I got the following tables in my database
Product(product_id,title,sale_price,tax_id)
Tax(Tax_id,label,rate)
I'm trying to show a list of products ordered by product.sale_price*tax.rate
How can I achieve that with Code Igniter
For the moment, my code looks like this :
$this->db->order_by('sale_price', 'asc');
$this->db->get('product');
And I tried to do something like this in SQL, but it doesn't work.
SELECT * FROM product inner join tax on product.tax_id = tax.tax_id ORDER BY (product.sale_price*tax.rate) ASC
Thank you.
回答1:
Try below query
$this->db->select('*');
$this->db->select('p.sale_price*t.rate as sortable_column');
$this->db->order_by('sortable_column', 'asc');
$this->db->join('tax t', 't.tax_id = p.tax_id');
$this->db->get('product p')->result_array();
来源:https://stackoverflow.com/questions/53259070/order-by-multiplication-in-code-igniter-query-builder