Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter Active Records to compare two columns

My table looks something like this

id o_name amount discount overall
1 first 10 0 10
2 second 20 20 40
3 third 0 0 0
4 fourth 40 40 80

SQL FIDDLE

I am trying to fetch data from table when the fields amount and discount are same and are not equal to zero.

So using CI, I wrote this

$this->db->select('count( id ) AS total_discounted', FALSE);
$this->db->where('amount', 'discount');     
$this->db->where('discount !=', 0);
$this->db->where('amount !=', 0);

$results = $this->db->get('user_orders');
echo $this->db->last_query();

which produces query

SELECT * FROM user_orders
WHERE amount = 'discount'
AND amount != 0 AND discount != 0;

where amount gets compared to the string 'discount' and not the column discount.

This is what I'm trying to obtain:

SELECT * FROM user_orders
WHERE amount = discount
    AND amount!= 0 AND discount!= 0;

How to achieve this using CI?

like image 391
Sajeev C Avatar asked Jun 17 '26 23:06

Sajeev C


1 Answers

Try $this->db->where('amount = discount');

like image 57
Disha V. Avatar answered Jun 19 '26 13:06

Disha V.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!