0

我该如何使用 left JOIN Table2 using(table.id)

我的代码示例

$this->db->select('visits.*,patients.name,workers.dr_name,time(visits.time)');
$this->db->from('visits');
//The next join = LEFT JOIN workers ON visits.worker_id=workers.worker_id
$this->db->join('workers','visits.worker_id=workers.worker_id','left');//WORKING
//The next join = JOIN `patients` ON patient_id --> i want it JOIN patients USING(patient_id)
$this->db->join('patients','patient_id','USING');//NOT WORKING

我搜索了每一个,但找不到解决方案,所以我打开并尝试在 db_active_rec.php 中编辑 JOIN 函数

/system/database/DB_active_rec.php

并找到了加入功能

public function join($table, $cond, $type = '')
    {
        if ($type != '')
        {
            $type = strtoupper(trim($type));

            if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER')))
            {
                $type = '';
            }
            else
            {
                $type .= ' ';
            }
        }

        // Extract any aliases that might exist.  We use this information
        // in the _protect_identifiers to know whether to add a table prefix
        $this->_track_aliases($table);

        // Strip apart the condition and protect the identifiers
        if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match))
        {
            $match[1] = $this->_protect_identifiers($match[1]);
            $match[3] = $this->_protect_identifiers($match[3]);

            $cond = $match[1].$match[2].$match[3];
        }

        // Assemble the JOIN statement
            $join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond;  


        $this->ar_join[] = $join;
        if ($this->ar_caching === TRUE)
        {
            $this->ar_cache_join[] = $join;
            $this->ar_cache_exists[] = 'join';
        }

        return $this;
    }

尝试编辑“//组装JOIN语句”下的部分并放置ifcondition来检测USING然后相应地调整查询但失败..史诗失败

有人可以帮忙吗?我怎样才能编辑这个函数,以便它在加入查询中使用 USING ?

4

1 回答 1

1

手册

$this->db->join();

允许您编写查询的 JOIN 部分:

$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');

$query = $this->db->get();

// Produces: 
// SELECT * FROM blogs
// JOIN comments ON comments.id = blogs.id

如果您需要在一个查询中进行多个联接,则可以进行多个函数调用。

如果您需要特定类型的 JOIN,您可以通过函数的第三个参数指定它。选项有:左、右、外、内、左外和右外。

$this->db->join('comments', 'comments.id = blogs.id', 'left');

// Produces: LEFT JOIN comments ON comments.id = blogs.id
于 2012-12-05T18:49:57.347 回答