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 関数を開いて編集しようとしました

/システム/データベース/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

1 つのクエリで複数の結合が必要な場合は、複数の関数呼び出しを行うことができます。

特定のタイプの JOIN が必要な場合は、関数の 3 番目のパラメーターを介して指定できます。オプションは、左、右、外側、内側、左外側、右外側です。

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

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