ajax在php中搜索多表连接

fivyi3re  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(348)

我使用ajax来实现搜索功能。存在依赖的多表联接。我没有得到正确的结果。我需要唯一的搜索结果。下面给出了我的代码:

$this->db->distinct('table2.sname,table3.cname');
$this->db>select('table1.stname,table2.*,table3.*,table4.*,table5.*');
$this->db->from('table5');
$this->db->join('table1','table1.stid=table2.stid');
$this->db->join('table2','table2.sid=table3.sid');
$this->db->join('table3','table3.cid=table4.cid');
$this->db->join('table4','table4.tid=table5.tid');
$this->db->or_like("table1.stname",$keyword);
$this->db->or_like("table2.sname",$keyword);
$this->db->or_like("table3.cname",$keyword);
$this->db->or_like("table4.tname",$keyword);
$this->db->or_like("table5.stoname",$keyword);
$query = $this->db->get();
i1icjdpr

i1icjdpr1#

如果您需要第一行表单select,那么可以使用limit(1)

<?php 

    $this->db->distinct('table2.sname,table3.cname');
     $this->db>select('table1.stname,table2.*,table3.*,table4.*,table5.*');
     $this->db->from('table5');
     $this->db->join('table1','table1.stid=table2.stid');
     $this->db->join('table2','table2.sid=table3.sid');
     $this->db->join('table3','table3.cid=table4.cid');
     $this->db->join('table4','table4.tid=table5.tid');
     $this->db->or_like("table1.stname",$keyword);
     $this->db->or_like("table2.sname",$keyword);
     $this->db->or_like("table3.cname",$keyword);
     $this->db->or_like("table4.tname",$keyword);
     $this->db->or_like("table5.stoname",$keyword);

    $this->db->limit(1);

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

?>

或者如果需要所有查询结果,则返回result()

<?php 

    $this->db->distinct('table2.sname,table3.cname');
     $this->db>select('table1.stname,table2.*,table3.*,table4.*,table5.*');
     $this->db->from('table5');
     $this->db->join('table1','table1.stid=table2.stid');
     $this->db->join('table2','table2.sid=table3.sid');
     $this->db->join('table3','table3.cid=table4.cid');
     $this->db->join('table4','table4.tid=table5.tid');
     $this->db->or_like("table1.stname",$keyword);
     $this->db->or_like("table2.sname",$keyword);
     $this->db->or_like("table3.cname",$keyword);
     $this->db->or_like("table4.tname",$keyword);
     $this->db->or_like("table5.stoname",$keyword);

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

     return $query->result();

?>

相关问题