我正在使用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();
如果你需要第一行的表单选择,那么你可以使用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();
?>
或者如果你需要所有查询结果,则返回结果((
<?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();
?>