[英]ajax search for multiple table join in php
我正在使用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();
?>
或者如果您需要所有查詢結果,則返回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();
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.