當我使用CI的活動記錄執行聯接時,有時結果不會顯示所有字段。例如,當使用具有相同字段名稱的表執行聯接時,表格字段中的一個字段不會顯示在結果中。codeigniter活動記錄:執行聯接時丟失的字段
實施例:
=== TABLE users ===
id INT(10)
first_name VARCHAR(255)
last_name VARCHAR(255)
=== TABLE messages ===
id INT(10)
exp_id INT(10)
dest_id INT(10)
content TEXT
=== CONTENT OF THE DATABASE ===
INSERT INTO users VALUES (1, "Bob", "Smith");
INSERT INTO users VALUES (2, "Graig", "Anderson");
INSERT INTO messages VALUES (999, 1, 2, "Hello Graig!");
=== the query ===
$this->db->where('messages.id', 999);
$this->db->join('users exp', 'users.id=messages.exp_id', 'INNER');
$this->db->join('users dest', 'users.id=messages.dest_id', 'INNER');
$res = $this->db->get('messages');
== the result ===
id exp_id dest_id content first_name last_name
999 1 2 Hello Graig! Bob Smith
我不能retreive既有 「dest.first_name」 和 「dest.first_name」。表「dest」位於join()中,但與表「exp」存在衝突,該表也具有「first_name」和「last_name」字段。事實上,這不是一個CI問題,這是MySQL的行爲。
它可以使用select/aliases修復,但是有沒有辦法使optionn將一個連接表的別名附加(或前置)到它的字段?因此,其結果可能是這樣的:
id exp_id dest_id content id_exp first_name_exp last_name_exp id_dest first_name_dest last_name_dest
999 1 2 Hello Graig! 1 Bob Smith 2 Graig Anderson