codeigniter 在WHERE条件中使用BETWEEN

1tu0hz3e  于 2022-12-07  发布在  其他
关注(0)|答案(8)|浏览(208)

我想使用以下函数来选择住宿介于$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();

   }
ctrmrzij

ctrmrzij1#

您应该使用

$this->db->where('$accommodation >=', minvalue);
$this->db->where('$accommodation <=', maxvalue);

我对语法不太清楚,所以如果不对的话请原谅。
无论如何,BETWEEN是使用〉=min &&〈=max来实现的。
这就是我举例子的意义。

编辑日期:

看一下this link,我想您可以这样写:

$this->db->where("$accommodation BETWEEN $minvalue AND $maxvalue");
2eafrhcq

2eafrhcq2#

在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)).'"');
2skhul33

2skhul333#

听起来正确,但执行此查询时可能会出现一些问题:我建议:

$this->db->where( "$accommodation BETWEEN $minvalue AND $maxvalue", NULL, FALSE );
mtb9vblg

mtb9vblg4#

您可能还会遇到错误消息。“操作数类型冲突:日期与整数不兼容
使用单引号括住日期。例如:$this->db->where("$accommodation BETWEEN '$minvalue' AND '$maxvalue'");

4sup72z8

4sup72z85#

我想我们可以这样写:这是一个很好的例子。在这里,我们使用了一个新的函数:
//不带美元($)符号这对我很有用:)

u1ehiz5o

u1ehiz5o6#

检查以下代码。它对我有效

$dateStart = $this->input->post('dateStart);
$dateTo= $this->input->post('dateTo);
$fromDate=date('Y-m-d',strtotime($dateStart));
$toDate=date('Y-m-d',strtotime($dateTo));
    
$this->db->from('tbl_sales sl');
$this->db->where('DATE(sl.created) >=',$setDate);
$this->db->where('DATE(sl.created) <=',$dateend);
$this->db->get();
r3i60tvu

r3i60tvu7#

// Codeigniter  3.1.11  

  public function getContraMarking($from_date2='',$to_date2='',$acctno2='')
    {
        if($from_date2!='' && $to_date2!='' && $acctno2!='')
        {
            $this->db->select('account_no,txn_date,SUM(pd_amt) AS debit,SUM(cr_amt) AS credit,COUNT(value_date) AS total_count');
            $this->db->from('ut_sbi_reco_paid_master');
            $this->db->where('account_no', $acctno2);
            $this->db->where('txn_date BETWEEN "'. $from_date2. '" AND "'. $to_date2. '" ');
            $this->db->group_by('account_no,txn_date,pd_amt');
            $this->db->order_by("txn_date");

            $query = $this->db->get();
            
            if($query->num_rows() > 0){
                return $query->row();
            }else{  
                return FALSE;
            }
        }
    }
sz81bmfz

sz81bmfz8#

在这里,您可以使用以下语句:
这是我解决方案

相关问题