简体   繁体   English

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

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

I have 5 tables and I want to inner join them but my codes doesn't work. 我有5个表,我想内部连接它们,但是我的代码不起作用。 Can someone help me? 有人能帮我吗? Thanks 谢谢

$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();

I think the problem is in the select call. 我认为问题出在select通话中。 Query Builder is probably not "escaping" your list of fields correctly which results in an invalid statement. 查询生成器可能没有正确“转义”您的字段列表,这导致了无效的语句。

Since you want all fields from all five tables this should work. 由于您希望所有五个表中的所有字段都可以使用。

$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();

The above can be made a little bit more concise and still produce the same result. 可以使上面的内容更简洁一些,并且仍然产生相同的结果。 The Query Builder method get() can accept the name of a table. 查询生成器方法get()可以接受表的名称。 So use 所以用

->get('tblmastersection') 

instead of 代替

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

Also, know that if a select() method is not provided then SELECT * is assumed. 另外,要知道,如果未提供select()方法,则假定使用SELECT *

Lastly, assigning the return from the chained methods to $query provides no benefit unless you want to check that $query is valid before using it. 最后,将链接方法的返回值分配给$query不会带来任何好处,除非您想在使用$query之前检查它是否有效。 Since you're not checking $query don't use it. 由于您没有检查$query请不要使用它。 Just go ahead and add ->result() to the chain and return the outcome immediately. 只需继续,在链中添加->result()并立即返回结果。

Here's the rewrite using that knowledge. 这是使用该知识进行的重写。

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