【问题标题】:Select records by month in where clause在 where 子句中按月选择记录
【发布时间】:2015-05-30 09:40:06
【问题描述】:

我的查询按月获取数据

$this->db->select('vehicle_make_and_model');
$this->db->join('tukai_drivers','tukai_drivers.driver_id=reqn_challans.dc_driver_id','left');
$this->db->where('dc_challan_date','MONTH(2)');
$this->db->order_by('dc_challan_id','desc');
$result=$this->db->get('reqn_challans')->result();

我正在像这样通过 codeignitor 尝试它

$this->db->where('dc_challan_date','MONTH(2)');

没用?怎么了

像这样存储日期

http://awesomescreenshot.com/0424q72ne5

【问题讨论】:

    标签: mysql codeigniter


    【解决方案1】:

    您需要从

    更新您的 where 条件
    $this->db->where('dc_challan_date','MONTH(2)');
    

    $this->db->where('MONTH(dc_challan_date)','2');
    

    实际函数类似于MONTH(date),所以这里有MONTH(dc_challan_date),这是你的列名

    【讨论】:

    • 虽然这个答案可能会更正语法,但它忽略了在 where 子句中对数据使用函数的问题。研究“sargable”一词。
    【解决方案2】:

    在 SQL 数据库中,您应该将函数应用于数据以适应 where 子句,以便最大限度地发挥索引的优势(即获得更好的性能)。例如,代替使用 MONTH(dc_challan_date) 您可以使用 >= 和

    select * from table
    where dc_challan_date >= '2015-02-01'
    and dc_challan_date < '2015-03-01'
    

    数据库优化器可以在该查询中使用列 dc_challan_date 的索引。该示例使用“sargable 谓词”。

    【讨论】:

      猜你喜欢
      • 2017-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-28
      • 1970-01-01
      相关资源
      最近更新 更多