我会直言不讳.
我的表看起来像这样
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
我想从表中提取数据时域amount
和discount
是相同的,不等于零.
所以使用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实现这一点?有线索吗?
尝试 $this->db->where('amount = discount');