php Codeigniter 活动记录:大于语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4134799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter Active record: greater than statement
提问by stef
I'm trying to convert a "greater than" where statement to CI's Active Record syntax. When I use this snippet
我正在尝试将“大于” where 语句转换为 CI 的 Active Record 语法。当我使用这个片段时
$this->db->join('product_stocks', "product_stocks.size_id_fk = product_attributes.id", "left");
$this->db->where('product_stocks.stock_level', '> 1');
$result = $this->db->get('product_attributes')->result_array();
Then print $this->db->last_query(); shows WHERE
product_stocks.
stock_level= '> 1'
which is of course not correct. Can this be done?
然后打印 $this->db->last_query(); 显示WHERE
product_stocks .
stock_level= '> 1'
这当然是不正确的。这能做到吗?
回答by JDM
I think this should do the trick:
我认为这应该可以解决问题:
$this->db->where('product_stocks.stock_level >', '1'); //moved the >
回答by Refazul Refat
Either
任何一个
$this->db->where('product_stocks.stock_level >', '1');
或者 $this->db->where(array('product_stocks.stock_level >'=>'1'));
应该这样做。注意字段名和操作符之间的空格;否则你最终会得到错误 1064。回答by Praburam S
You Can also Pass a String parameter to where() function like this,
您还可以像这样将字符串参数传递给 where() 函数,
$this->db->where('product_stocks.stock_level > 1');
回答by Timo
I would like to have in CI the following:
我希望在 CI 中有以下内容:
$sQuery = "SELECT auftrag, saal, DATE_FORMAT(konzertdatum,'%e, %M, %Y') AS konzertdatum2 FROM auftrag
JOIN saal on auftrag.saal_id = saal.id
WHERE konzertdatum < NOW() + INTERVAL 240 DAY AND auftrag like '%$sWord%' order by konzertdatum asc LIMIT 4";
$aOrder = $this->db->query($sQuery);
$aOrder = $aOrder->result();
It works fine without CI, but when I use
它在没有 CI 的情况下工作正常,但是当我使用
$this->db->select("auftrag, saal, DATE_FORMAT(konzertdatum,'%e, %M, %Y') AS konzertdatum2", false );
$this->db->from('auftrag');
$this->db->join('saal', 'auftrag.saal_id = saal.id');
$this->db->like('auftrag', $sWord);
$this->db->where('konzertdatum <', 'NOW() + Interval 240 day');
$this->db->order_by('konzertdatum');
$this->db->limit(4);
$oQuery = $this->db->get();
$aOrder = $oQuery->result();
print_r($this->db->last_query());
it returns all results not caring about the where (although the sql seems fine):
它返回所有不关心 where 的结果(尽管 sql 看起来不错):
SELECT auftrag, saal, DATE_FORMAT(konzertdatum, '%e, %M, %Y') AS konzertdatum2 FROM (`auftrag`) JOIN `saal` ON `auftrag`.`saal_id` = `saal`.`id` WHERE `konzertdatum` < 'NOW() + Interval 240 day' AND `auftrag` LIKE '%spangenberg%' ORDER BY `konzertdatum` LIMIT 4