drwxg62286 2015-12-15 21:12
浏览 36
已采纳

CodeIgniter Active Records比较同一个mysql表的两列

I'll get straight into the point.

My table looks something like this

CREATE TABLE user_orders(id INT(11), o_name VARCHAR(60), amount INT(10), discount INT (10), overall INT(10));
INSERT INTO user_orders(id,o_name,amount,discount,overall) VALUES (1,'first', 10,0,10);
INSERT INTO user_orders(id,o_name,amount,discount,overall) VALUES (2,'second', 20,20,40);
INSERT INTO user_orders(id,o_name,amount,discount,overall) VALUES (3,'third', 0,0,0);
INSERT INTO user_orders(id,o_name,amount,discount,overall) VALUES (4,'fourth', 40,40,80);

And this is what I'm tryna obtain:

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

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 field discount.

How to achieve this kinda thing using CI? Any clues?

  • 写回答

3条回答 默认 最新

  • dpnof28482 2015-12-15 21:43
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?