【问题标题】:Codeigniter Where with ANDCodeigniter 在哪里使用 AND
【发布时间】:2018-11-23 13:25:03
【问题描述】:

我想用这样的 codeigniter 构建一个查询。

SELECT * FROM table WHERE machine=$var AND date BETWEEN '$today' AND '$oneMonthAgo'

这里是我的模态

public function getHistory($machine)
    {
    $today = date('Y-m-d');

    $month=date('Y-m-d',strtotime("-1 month"));
    $query = $this->db->from('table')->where('machine', $machine)->where("date BETWEEN '$today' AND '$month'")->get();

        if ($query->num_rows() > 0) {
            return $query->row();
        }
    }

我一直在尝试,但我没有得到结果 加:用这条线可以计算一个月前的日期

$today = date('Y-m-d');

$month=date('Y-m-d',strtotime("-1 month"));

对不起我的英语xP

【问题讨论】:

  • 为什么不使用date < '$today' AND date > '$month'

标签: php codeigniter


【解决方案1】:

请尝试以下代码:-

public function getHistory($machine)
{
    $today = date('Y-m-d');
    $query = $this->db->from('table')
             ->where('machine', $machine)
             ->where('date >=', (NOW() - INTERVAL 1 MONTH))
             ->get();
    if ($query->num_rows() > 0) 
    {
      return $query->result();
    }
}

【讨论】:

    【解决方案2】:

    你可以用这个。

    $this->db->query(your query);
    

    喜欢。

    $sql = "SELECT * FROM table WHERE machine=$var AND date BETWEEN '$today' AND '$oneMonthAgo'";
    $this->db->query($sql);
    

    【讨论】:

      【解决方案3】:

      试试这个:

      public function getHistory($machine)
      {
      $today = date('Y-m-d');
      
      $month=date('Y-m-d',strtotime("-1 month"));
      $query = $this->db->from('table')->where("machine= '$machine' AND date BETWEEN '$today' AND '$month'")->get();
      
          if ($query->num_rows() > 0) {
              return $query->row();
          }
      }
      

      希望它有效...

      【讨论】:

        【解决方案4】:

        试试这个。希望对你有帮助。

        return $this->db->where("machine = '" . $machine . "' AND date >= '" . $today . "' AND date <= '" . $$month . "'")->get('table')->result_array();
        

        【讨论】:

          【解决方案5】:

          试试这个:

          public function getHistory($machine)
          {
              $today = date('Y-m-d'); 
              $month = date('Y-m-d', strtotime('-1 month'));
          
              $query = $this->db
                ->where('machine', $machine)
                ->where('DATE(date) <=', $today)
                ->where('DATE(date) >=', $month)
                ->get('table');
          
              if ($query->num_rows() > 0) 
              {
                return $query->result();
              }
          }
          

          参考:https://www.codeigniter.com/user_guide/database/query_builder.html#looking-for-specific-data

          自定义查询

          public function getHistory($machine)
          {
              $today = date('Y-m-d'); 
              $month = date('Y-m-d', strtotime('-1 month'));
          
              $sql = "SELECT * FROM table WHERE machine={$var} AND date BETWEEN {$today} AND {$oneMonthAgo}"
              $query = $this->db->query($sql);
          
              if ($query->num_rows() > 0) 
              {
                return $query->result();
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-11-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-10-10
            相关资源
            最近更新 更多