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

时间:2015-12-16 05:12:49

标签: php codeigniter

我会直截了当地说。

我的表看起来像这样

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);

这就是我试图获得的:

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

SQL FIDDLE

我想在字段amountdiscount相同且不等于零时从表中获取数据。

所以使用CI,我写了这个

    $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();

产生查询

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

将金额与THE STRING' discount'进行比较而不是字段discount

如何使用CI实现这一点?有线索吗?

3 个答案:

答案 0 :(得分:5)

尝试user_logged_in.connect(update_last_login)

答案 1 :(得分:0)

试试这个:

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

答案 2 :(得分:0)

在Codeigniter 3中:

id

第三个参数将为disable escaping