gpt4 book ai didi

php - Codeigniter 在 SQL 查询中使用 LIKE 和 WHERE

转载 作者:可可西里 更新时间:2023-11-01 07:58:31 24 4
gpt4 key购买 nike

在 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();
}

最佳答案

For 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();

For 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');

关于php - Codeigniter 在 SQL 查询中使用 LIKE 和 WHERE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31667135/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com