我需要选择几个符合以下条件的行:
我想为此的SQL语句应该是:
SELECT * FROM `events` WHERE date >= '2010-09-12' AND (field1 LIKE '%keyword%' OR field2 LIKE '%keyword%' OR field3 LIKE '%keyword%')
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用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');
Run Code Online (Sandbox Code Playgroud)
我可能需要在括号内插入LIKE语句,但不知道该怎么做。
我认为您必须以这种方式编写查询的“喜欢”部分:
$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');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23061 次 |
| 最近记录: |