Order by “multiplication” in code igniter query builder

馋奶兔 提交于 2020-03-05 07:27:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!