ajax在php

时间:2018-09-22 12:47:05

标签: php jquery mysql ajax codeigniter

我正在使用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();

1 个答案:

答案 0 :(得分:0)

如果需要选择第一行表格,则可以使用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();

?>