mysql - 我如何从两个不同的表中搜索数据?

标签 mysql search join

下面是我的代码。 型号。

function get_search_conv() 
    {   

          $match = $this->input->post('search');
          $this->db->like('TextDecoded',$match);
          $query = $this->db->get('inbox');
          return $query->result();

    }

它适用于一张表,但如何在表“inbox2”中搜索 textDecoded? 我尝试过加入工会或加入,但仍然不起作用。有人可以帮助我吗? 谢谢

最佳答案

你可以这样做

$sql="select * from inbox in1, inbox2 in2 
          where in1.TextDecoded like '%".$match."%' 
            or in2.TextDecoded like '%".$match."%'";
    $query = $this->db->query($sql);
    return $query->result();

另一种可能的解决方案是

function get_search_conv() 
{   

      $match = $this->input->post('search');
      $this->db->like('TextDecoded',$match);
      $this->db->or_like('TextDecoded',$match);
      $this->db->from('inbox,inbox2');
      $query = $this->db->get();
      return $query->result();

}

这是您的另一个答案。避免使用 Active Record 执行此操作,因为您必须将 DB_active_rec.php 中的函数 _compile_select() 和 _reset_select() 的访问修饰符更改为 public。这不是一个好主意

$sql = "SELECT TextDecoded FROM inbox where TextDecoded like '%".$match."%'
UNION 
SELECT TextDecoded FROM inbox2 where TextDecoded like '%".$match."%'"

$query = $this->db->query($sql);

return $query->result();

关于mysql - 我如何从两个不同的表中搜索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23623855/

相关文章:

JavaScript:检查字符串是否包含 '.'(句号)

ruby-on-rails - Rails 3 - 具有连接条件的多数据库

mysql - 根据左连接列值返回 View 中的一行

mysql - 是否可以从 MySQL 表返回数组?

mysql - 如何快速重命名 MySQL 数据库(更改架构名称)?

php - 从 php 搜索返回图像

mysql - rails3 中的 activerecord 不返回连接表中的多列

java - 如何解决jsp页面中的这种错误?

mysql - 使用 source 命令会破坏非 Unicode 文本编码

c++ - 在指针 vector 中搜索字符串