简体   繁体   English

将 mysql 查询转换为 codeigniter 3 模型

[英]convert the mysql query to codeigniter 3 model

Convert the MySQL query to Codeigniter Query将 MySQL 查询转换为 Codeigniter 查询

    $query = "(SELECT content, title, 'msg' as type FROM messages WHERE content LIKE '%" . 
               $keyword . "%' OR title LIKE '%" . $keyword ."%') 
               UNION
               (SELECT content, title, 'topic' as type FROM topics WHERE content LIKE '%" . 
               $keyword . "%' OR title LIKE '%" . $keyword ."%') 
               UNION
               (SELECT content, title, 'comment' as type FROM comments WHERE content LIKE '%" . 
               $keyword . "%' OR title LIKE '%" . $keyword ."%')";

    mysql_query($query);

I have tried to convert it in Codeigniter我试图在 Codeigniter 中转换它

              $this->db->like("content", $keyword);

              $this->db->or_like('title',$keyword,'after'); 
              $this->db->or_like('msg',$keyword,'after'); 
             ->from('message')
              $this->db->like("content", $keyword);
              $this->db->or_like('title',$keyword,'after'); 
              $this->db->like("msg", $keyword);
             ->from('topics')
              $this->db->or_like('content',$keyword,'after'); 
              $this->db->or_like('title',$keyword,'after'); 
              $this->db->or_like('msg',$keyword,'after'); 
              ->from('comment')

The top one is in MySQL and bottom which I try to convert is in Codeigniter I m trying to search the keyword from selected columns from three tables.最上面的一个在 MySQL 中,我尝试转换的底部在 Codeigniter 我试图从三个表的选定列中搜索关键字。 How I can convert the MySQL to Codeigniter.如何将 MySQL 转换为 Codeigniter。 I'm trying to search the keyword from selected columns from three tables.我正在尝试从三个表的选定列中搜索关键字。

How I can convert the MySQL to Codeigniter我如何将 MySQL 转换为 Codeigniter

Try this尝试这个

$this->db->select('content, title, msg as type');
$this->db->from('message');
$this->db->like("content", $keyword);
$this->db->or_like('title',$keyword,'after'); 
$this->db->or_like('msg',$keyword,'after'); 
$query1 = $this->db->get_compiled_select();


$this->db->select('content, title, msg as type');
$this->db->from('topics');
$this->db->like("content", $keyword);
$this->db->or_like('title',$keyword,'after'); 
$this->db->like("msg", $keyword);
$query2 = $this->db->get_compiled_select();

$this->db->select('content, title, msg as type');
$this->db->from('comment');
$this->db->or_like('content',$keyword,'after'); 
$this->db->or_like('title',$keyword,'after'); 
$this->db->or_like('msg',$keyword,'after'); 
$query3 = $this->db->get_compiled_select();

$result = $this->db->query($query1." UNION ".$query2." UNION ".$query3);
return $result->result();

Note:- If you intend to use this make sure that your both the table column are same sequence and name.注意:-如果您打算使用它,请确保您的两个表列具有相同的序列和名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM