Codeigniter活动记录:喜欢和SQL语句中的位置

时间:2010-09-12 13:50:35

标签: codeigniter

我需要选择几个符合这些条件的行:

  1. 日期> =今天
  2. 关键字出现在字段1,2或3
  3. 我猜这个SQL语句应该是:

    SELECT * FROM `events` WHERE date >= '2010-09-12' AND (field1 LIKE '%keyword%' OR field2 LIKE '%keyword%' OR field3 LIKE '%keyword%')
    

    我正在尝试使用codeigniter的活动记录来写这个,但LIKE条件似乎会覆盖日期。

    $this->db->select('*');
    $this->db->join('venues', 'events.venue = venue_id');
    
    //first condition: date >= today
    $this->db->where('date >=', date('Y-m-d'));
    
    if ($keyword)
    //if keyword is present, add this condition as well
    {
        $this->db->like('events.description', $keyword);
        $this->db->or_like('band', $keyword);
        $this->db->or_like('venues.venue', $keyword);
    }
    
    $this->db->order_by('date', 'ASC');
    $this->db->order_by('events.priority', 'DESC'); 
    $Q = $this->db->get('events');
    

    我可能需要在肠胃切除术中插入LIKE语句,但不知道该怎么做。

3 个答案:

答案 0 :(得分:5)

我认为你必须以这种方式编写查询的“喜欢”部分:

$this->db->select('*');
$this->db->join('venues', 'events.venue = venue_id');

if ($keyword)
//if keyword is present, add this condition as well
{
   $where = "( events.description LIKE '$keyword' OR band LIKE '$keyword' OR venues.venue LIKE '$keyword')";
   $this->db->where($where);

}
//first condition: date >= today
$this->db->where('date >=', date('Y-m-d'));

$this->db->order_by('date', 'ASC');
$this->db->order_by('events.priority', 'DESC'); 

答案 1 :(得分:0)

$array = array('title' => $match, 'page1' => $match, 'page2' => $match);

$this->db->like($array);

// WHERE title LIKE '%match%' AND page1 LIKE '%match%' AND page2 LIKE '%match%'

答案 2 :(得分:0)

$Linkearray = array('page_content' => $search, 'page_title' => $search);
$this->db->like($Linkearray);
$this->db->where('page_online',1);
$this->db->where('page_site',$page_site);

// WHERE page_content LIKE '% my Text%' 
// AND page_title LIKE '% my page title %' 
// AND page_online = '1' and page_site ='29'