php 在 WHERE 条件下使用 BETWEEN
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9941521/
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
using BETWEEN in WHERE condition
提问by Viju Vijay
I'd like the following function to select hotels with an accomodation between a certain $minvalue
and $maxvalue
. What would be the best way to do that?
我想要以下功能来选择住宿在某个$minvalue
和$maxvalue
. 什么是最好的方法呢?
function gethotels($state_id,$city,$accommodation,$minvalue,$maxvalue,$limit,$pgoffset)
{
$this->db->limit($limit, $pgoffset);
$this->db->order_by("id", "desc");
$this->db->where('state_id',$state_id);
$this->db->where('city',$city);
// This one should become a between selector
$this->db->where($accommodation,$minvalue);
$result_hotels = $this->db->get('hotels');
return $result_hotels->result();
}
回答by Marco
You should use
你应该使用
$this->db->where('$accommodation >=', minvalue);
$this->db->where('$accommodation <=', maxvalue);
I'm not sure of syntax, so I beg your pardon if it's not correct.
Anyway BETWEEN
is implemented using >=min && <=max.
This is the meaning of my example.
我不确定语法,所以如果它不正确,请原谅。
无论如何BETWEEN
都是使用>=min && <=max 实现的。
这就是我的例子的意义。
EDITED:
Looking at this linkI think you could write:
编辑:
查看此链接,我认为您可以写:
$this->db->where("$accommodation BETWEEN $minvalue AND $maxvalue");
回答by Muhammad Fahad
In Codeigniter This is simple Way to check between two date records ...
在 Codeigniter 这是检查两个日期记录之间的简单方法...
$start_date='2016-01-01';
$end_date='2016-01-31';
$this->db->where('date BETWEEN "'. date('Y-m-d', strtotime($start_date)). '" and "'. date('Y-m-d', strtotime($end_date)).'"');
回答by syyu
Sounds correct but some issues maybe creates executing this query: I would suggest:
听起来正确,但执行此查询可能会产生一些问题:我建议:
$this->db->where( "$accommodation BETWEEN $minvalue AND $maxvalue", NULL, FALSE );
回答by user3593238
You might also encounter an error message. "Operand type clash: date is incompatible with int.
您可能还会遇到错误消息。“操作数类型冲突:日期与整数不兼容。
Use single quotes around the dates. E.g.: $this->db->where("$accommodation BETWEEN '$minvalue' AND '$maxvalue'");
在日期周围使用单引号。例如:$this->db->where("$accommodation BETWEEN '$minvalue' AND '$maxvalue'");
回答by Dek Sudiana
I think we can write like this : $this->db->where('accommodation >=', minvalue); $this->db->where('accommodation <=', maxvalue);
我想我们可以这样写: $this->db->where('accommodation >=', minvalue); $this->db->where('accommodation <=', maxvalue);
//without dollar($) sign It's work for me :)
//没有美元($)符号这对我有用:)