0

我想从我的两个日期选择器之间的数据库中选择所有数据。请帮助我了解如何在这种情况下使用 BETWEEN AND 语句。我从第一个日期选择器中选择一个日期,然后显示数据,但是当我在第二个日期选择器上选择一个日期时,第二个日期选择器中的日期数据只显示第一个日期消失了

我用了这个查询

$this->db->where('date_sold >=',$this->input->post('selectdate'));

$this->db->where('date_sold <=',$this->input->post('selectdate2'));

但我只得到1个数据..

<span>Select 1st sem:</span>
                <div id="datetimepicker1" class="input-append date">
                    <input data-format="MM/dd/yyyy" type="text" readonly name="selectDate" id="selectDate"></input>                                 
                    <span class="add-on">
                      <i data-time-icon="icon-time" data-date-icon="icon-calendar">
                      </i>                                                        
                    </span>
                </div>
                <script type="text/javascript">

                 $(function(){ 
                    var date = new Date();
                    date.setDate(date.getDate());

                      $('#datetimepicker1').datetimepicker({                            
                            pickTime: false
                      });

                  });
                </script>   

                <span>Select 2st sem:</span>
                <div id="datetimepicker2" class="input-append date">
                    <input data-format="MM/dd/yyyy" type="text" readonly name="selectDate" id="selectDate2"></input>                                    
                    <span class="add-on">
                      <i data-time-icon="icon-time" data-date-icon="icon-calendar">
                      </i>                                                        
                    </span>
                </div>

                <script type="text/javascript">

                 $(function(){ 
                    var date = new Date();
                    date.setDate(date.getDate());
                      $('#datetimepicker2').datetimepicker({                            
                            pickTime: false
                      });

                  });
                </script>   

/这是我的看法

    function get_semreportdata(){
    $q = $this->db->select('
             books.title,
             reserved_books.id,
             reserved_books.isbn, 
             reserved_books.price,
             reserved_books.item_sold,
             reserved_books.date_sold,
             reserved_books.total_amount
            ')
        ->from('reserved_books')
        ->join('books','reserved_books.isbn= books.isbn')
        ->where('date_sold >=',$this->input->post('selectdate'))
        ->where('date_sold <=',$this->input->post('selectdate2'));



        $reserved['rows'] = $q->get()->result();
        $this->db->select_sum('total_amount');
        $this->db->where('date_sold',$this->input->post('selectdate'));

        $query = $this->db->get('reserved_books');
        $reserved['total'] = $query->result();

        return $reserved;

}

这是我的模型

4

3 回答 3

1

在您的代码示例中,您的两个字段都命名为selectDate 。我认为这就是为什么您的原始查询不起作用的原因。尝试将第二个字段重命名为selectdate2

于 2013-10-24T13:09:14.013 回答
0

我会说这样的话:

“.$this->input->post('selectdate')”之间的“date_sold”在哪里。和“.$this->input->post('selectdate')。”

假设您的数据库表的提交日期格式正确。

于 2013-10-15T12:30:52.387 回答
0

试试这个,确保你有适当的date_sold日期或日期时间类型,并注意发布的输入,它们包含与date_sold列一样的确切日期格式。我已经制作了三种情况,一种用于介于之间,另外两种包含单个日期数据。我通过将第三个参数传递为 false 来使用自定义字符串

    $datef=$this->input->post('selectdate');
    $dates=$this->input->post('selectdate2');
    $q = $this->db->select('
             books.title,
             reserved_books.id,
             reserved_books.isbn, 
             reserved_books.price,
             reserved_books.item_sold,
             reserved_books.date_sold,
             reserved_books.total_amount
            ')
        ->from('reserved_books')
        ->join('books','reserved_books.isbn= books.isbn');

if(!empty($datef) && !empty($dates)){
$this->db->where(" `date_sold` BETWEEN '".$datef."' AND '".$dates."' ", NULL, FALSE); 

}elseif(!empty($datef)){
$this->db->where( " `date_sold` >=.'"$datef."' ", NULL, FALSE);

}elseif(!empty($dates)){

$this->db->where(" `date_sold` <=.'".$dates."' ", NULL, FALSE);
}

参考

$this->db->where() 接受可选的第三个参数。如果您将其设置为 FALSE,CodeIgniter 将不会尝试使用反引号保护您的字段或表名。

活动记录

于 2013-10-15T13:32:58.450 回答