phpcodeigniter

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', 'discounts');     
$this->db->where('discounts !=', 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?


Solution

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