Codeigniter 的此 JOIN 的兼容版本是什么

What is the compatible version of this JOIN for Codeigniter

我想要 JOINCodeigniter Query Builder 语言中的兼容版本。

我的代码工作正常,但我想学习如何做。

$query = $this->db->query("
        SELECT DISTINCT
        pics_tag.* , pics.*
        FROM pics_tag INNER JOIN pics
        ON pics_tag.pics_id = pics.id
        JOIN tag
        ON pics_tag.tag_id = $tag_id           
        ");
$this->db->select("pics_tag.*, pics.*");
$this->db->from("pics_tag");
$this->db->join("pics","pics_tag.pics_id=pics.id");
$this->db->join("tag","pics_tag.tag_id=$tag_id");

阅读 Codeigniter 文档以了解正确的连接查询

或者你可以创建模型,这样只有你必须在模型函数中给出的字段。

   function GetJoinRecordNew($table, $field_first, $tablejointo, $field_second, $field, $value, $field_val) {
        $this->db->select("$field_val");
        $this->db->from("$table");
        $this->db->join("$tablejointo", "$tablejointo.$field_second = $table.$field_first");
        $this->db->where("$table.$field", "$value");
        $this->db->group_by("$table.$field");
        $q = $this->db->get();
        if ($q->num_rows() > 0) {
            foreach ($q->result() as $rows) {
                $data[] = $rows;
            }
            $q->free_result();
            return $data;
        }
    }

现在你可以只传递参数而不需要为连接添加不同的函数。这是最好的方法。

我建议 group_by 然后使用 Distinct。

您需要添加group_by

$query = $this->db
    ->select('pt.* , p.*')  
    ->select('$subquery')   
    ->from('pics_tag as pt')
    ->join('pics as p', 'pt.pics_id = p.id', 'left')
    ->join('tag as t', 'pt.tag_id = '.$tag_id.'', 'left')       
    //->group_by('column_name'); // add group by    add your colun name and remove comment          
    ->get();