[英]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.