Codeigniter在SQL查询中使用LIKE和WHERE

时间:2015-07-28 04:00:37

标签: php mysql sql codeigniter

在codeigniter中,我有这样的查询:

    $this->db->select('*', false);
    $this->db->from('video as v');
    $this->db->where('v.start_date <=', 'NOW()', false);
    $this->db->like('v.title', $keywords);
    $this->db->or_like('v.title_tw', $keywords);
    $this->db->or_like('v.title_cn', $keywords);
    $query = $this->db->get();
    return $query->result_array();

在我的期望中:

start_date <= NOW **AND** title like $keywords OR title_tw like $keywords OR title_cn like $keywords

但事实证明是:

start_date <= NOW **OR** title like $keywords OR title_tw like $keywords OR title_cn like $keywords

如何解决这个问题?非常感谢。

更新:

实际查询:

function search_video($keywords) {
    $this->db->select('v.*,t.name,t.name_tw,t.name_cn, t.image_url as profile_pic, COALESCE(b.duration, bt.duration, bc.duration) AS duration, COALESCE(b.thumbnail, bt.thumbnail, bc.thumbnail) as thumbnail', false);
    $this->db->from('video as v');
    $this->db->join('teacher as t', 'v.teacher_id = t.id');
    $this->db->join('video_tag as vt', 'v.id = vt.video_id', 'left');
    $this->db->join('brightcove as b', 'v.video = b.video_id', 'left');
    $this->db->join('brightcove as bt', 'v.video_tw = bt.video_id', 'left');
    $this->db->join('brightcove as bc', 'v.video_cn = bc.video_id', 'left');
    $this->db->where('v.is_delete', false);
    $this->db->where('v.start_date <=', 'NOW()', false);
    $this->db->like('v.title', $keywords);
    $this->db->or_like('v.title_tw', $keywords);
    $this->db->or_like('v.title_cn', $keywords);
    $this->db->or_like('vt.tag', $keywords);
    $this->db->group_by('v.id');
    $query = $this->db->get();
    return $query->result_array();
}

3 个答案:

答案 0 :(得分:4)

  

对于CodeIgniter 3.x:

您可以使用Query grouping在查询中使用括号:

    $this->db->select('v.*,t.name,t.name_tw,t.name_cn, t.image_url as profile_pic, COALESCE(b.duration, bt.duration, bc.duration) AS duration, COALESCE(b.thumbnail, bt.thumbnail, bc.thumbnail) as thumbnail', false);
        $this->db->from('video as v');
        $this->db->join('teacher as t', 'v.teacher_id = t.id');
        $this->db->join('video_tag as vt', 'v.id = vt.video_id', 'left');
        $this->db->join('brightcove as b', 'v.video = b.video_id', 'left');
        $this->db->join('brightcove as bt', 'v.video_tw = bt.video_id', 'left');
        $this->db->join('brightcove as bc', 'v.video_cn = bc.video_id', 'left');
        $this->db->where('v.is_delete', false);
        $this->db->where('v.start_date <=', 'NOW()', false);
        $this->db->group_start();
        $this->db->like('v.title', $keywords);
        $this->db->or_like('v.title_tw', $keywords);
        $this->db->or_like('v.title_cn', $keywords);
        $this->db->or_like('vt.tag', $keywords);
        $this->db->group_end();
        $this->db->group_by('v.id');
        $query = $this->db->get();
  

对于CodeIgniter 2.x:

虽然我还没有对它进行测试,但我认为这应该可行:

$this->db->select('v.*,t.name,t.name_tw,t.name_cn, t.image_url as profile_pic, COALESCE(b.duration, bt.duration, bc.duration) AS duration, COALESCE(b.thumbnail, bt.thumbnail, bc.thumbnail) as thumbnail', false);
    $this->db->from('video as v');
    $this->db->join('teacher as t', 'v.teacher_id = t.id');
    $this->db->join('video_tag as vt', 'v.id = vt.video_id', 'left');
    $this->db->join('brightcove as b', 'v.video = b.video_id', 'left');
    $this->db->join('brightcove as bt', 'v.video_tw = bt.video_id', 'left');
    $this->db->join('brightcove as bc', 'v.video_cn = bc.video_id', 'left');
    $this->db->where('v.is_delete', false);
    $this->db->where('v.start_date <=', 'NOW()', false);
    $this->db->where("( v.title LIKE '%{$keywords}%'", null, FALSE);
    $this->db->or_like('v.title_tw', $keywords);
    $this->db->or_like('v.title_cn', $keywords);
    $this->db->where("OR vt.tag LIKE '%{$keywords}%' )", null, FALSE);
    $this->db->group_by('v.id');

答案 1 :(得分:1)

你可以这样试试:

$sql = SELECT * FROM video as v where v.start_date <= NOW() AND (v.title like ? OR v.title_tw like ? or v.title_cn like ?);
$query = $this->db->query($sql, array($keywords, $keywords, $keywords));

答案 2 :(得分:0)

最后我像这样创建查询,似乎工作正常:

function search_video($keywords) {
    $this->db->select('v.*,t.name,t.name_tw,t.name_cn, t.image_url as profile_pic, COALESCE(b.duration, bt.duration, bc.duration) AS duration, COALESCE(b.thumbnail, bt.thumbnail, bc.thumbnail) as thumbnail', false);
    $this->db->from('video as v');
    $this->db->join('teacher as t', 'v.teacher_id = t.id');
    $this->db->join('video_tag as vt', 'v.id = vt.video_id', 'left');
    $this->db->join('brightcove as b', 'v.video = b.video_id', 'left');
    $this->db->join('brightcove as bt', 'v.video_tw = bt.video_id', 'left');
    $this->db->join('brightcove as bc', 'v.video_cn = bc.video_id', 'left');
    $this->db->where("(v.is_delete = false AND v.start_date <= NOW()) AND (v.title LIKE '$keywords' OR  v.title_tw LIKE '$keywords' OR v.title_cn LIKE '$keywords' OR vt.tag LIKE '$keywords')");
    $this->db->group_by('v.id');
    $query = $this->db->get();
    return $query->result_array();
}

非常感谢帮助。