如何在联接表中搜索?

qeeaahzv  于 2021-06-23  发布在  Mysql
关注(0)|答案(2)|浏览(279)

我的表格示例:

customer (id , name ....)
transactions (id , sender , receiver .....)

发送方和接收方都与客户id相关
我坐在table旁边

$this->db->select('transactions.*', FALSE);

    $this->db->select('tbl_receiver.name as receiver_name,tbl_sender.name as sender_name');
    $this->db->join('customer as tbl_receiver','tbl_receiver.id=transactions.receiver');
    $this->db->join('customer as tbl_sender','tbl_sender.id=transactions.sender');

现在我可以显示一个表,比如(id,receiver\u name,sender\u name…)我需要一个搜索栏,可以搜索receiver/sender name

$this->db->like('receiver_name','Jackson');

但上面说没有收件人姓名这一栏。我如何让它能够按收件人名称搜索?

nle07wnf

nle07wnf1#

你失踪了 ' 在你的陈述中。应该是的

$this->db->like('receiver_name','Jackson');

你也可以这样写:

$this->db->select('*')->from('table')
       ->where("column LIKE '%$keyword%'")->get();
kwvwclae

kwvwclae2#

这是我的代码工作完美。你可以试试这个我希望这个对你有用。

public function index()
{
    /*$email=$this->post->input('search');*/ //OR

    $email='myEmail@test.com'; // Set email accroding you
    $this->db->select('*');
    $this->db->from('table1');
    $this->db->like('table1.email',$email);
    $this->db->join('table2', 'table1.email = table2.email');
    $query = $this->db->get();

    $assignedData=$query->result_array();

    var_dump($assignedData); // store data in array
}

相关问题