【问题标题】:CodeIgniter-Inner Join 5 or more Tables (SQL)CodeIgniter-Inner Join 5个或更多表(SQL)
【发布时间】:2018-03-07 09:21:42
【问题描述】:

我有 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();

【问题讨论】:

  • 一一加入并查看结果,以便您找到导致问题的表,或者将所有相关表与示例数据一起发布在您的帖子中。
  • 请发布您的表结构和查询输出。
  • “不起作用”是什么意思?

标签: php mysql sql codeigniter


【解决方案1】:

我认为问题出在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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-03
    • 2014-01-22
    • 1970-01-01
    • 2011-07-29
    • 2023-03-10
    相关资源
    最近更新 更多