繁体   English   中英

CodeIgniter-内部联接5个或更多表(SQL)

[英]CodeIgniter-Inner Join 5 or more Tables (SQL)

我有5个表,我想内部连接它们,但是我的代码不起作用。 有人能帮我吗? 谢谢

$query = $this->db->select('tblmastersection.*, tblsavesection.*,tblmastersecmolecular.*,tblmastersecpathology.*,tblmastersecparasitology.* ')
          ->from('tblmastersection')
          ->join('tblsavesection', 'tblmastersection.section_id = tblsavesection.section_id', 'inner')
           ->join('tblmastersecmolecular', 'tblsavesection.test_id = tblmastersecmolecular.test_id', 'inner')
          ->join('tblmastersecparasitology', 'tblsavesection.test_id = tblmastersecparasitology.test_id', 'inner')
            ->join('tblmastersecpathology', 'tblsavesection.test_id = tblmastersecpathology.test_id', 'inner')
            ->get();
    return $query->result();

我认为问题出在select通话中。 查询生成器可能没有正确“转义”您的字段列表,这导致了无效的语句。

由于您希望所有五个表中的所有字段都可以使用。

$query = $this->db
    ->select('*')
    ->from('tblmastersection')
    ->join('tblsavesection', 'tblmastersection.section_id=tblsavesection.section_id', 'inner')
    ->join('tblmastersecmolecular', 'tblsavesection.test_id = tblmastersecmolecular.test_id', 'inner')
    ->join('tblmastersecparasitology', 'tblsavesection.test_id = tblmastersecparasitology.test_id', 'inner')
    ->join('tblmastersecpathology', 'tblsavesection.test_id = tblmastersecpathology.test_id', 'inner')
    ->get();

return $query->result();

可以使上面的内容更简洁一些,并且仍然产生相同的结果。 查询生成器方法get()可以接受表的名称。 所以用

->get('tblmastersection') 

代替

->from('tblmastersection')
//and later in the chain
->get()

另外,要知道,如果未提供select()方法,则假定使用SELECT *

最后,将链接方法的返回值分配给$query不会带来任何好处,除非您想在使用$query之前检查它是否有效。 由于您没有检查$query请不要使用它。 只需继续,在链中添加->result()并立即返回结果。

这是使用该知识进行的重写。

return $this->db
    ->join('tblsavesection', 'tblmastersection.section_id=tblsavesection.section_id', 'inner')
    ->join('tblmastersecmolecular', 'tblsavesection.test_id = tblmastersecmolecular.test_id', 'inner')
    ->join('tblmastersecparasitology', 'tblsavesection.test_id = tblmastersecparasitology.test_id', 'inner')
    ->join('tblmastersecpathology', 'tblsavesection.test_id = tblmastersecpathology.test_id', 'inner')
    ->get('tblmastersection')
    ->result();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM