简体   繁体   English

CodeIgniter两个表左连接在右表中未命中时不返回左表的连接条件id

[英]CodeIgniter Two tables Left Join does not return Join Condition id of left table when miss in right table

I have two tables say Table1 and Table2 and i am Left joining Table1 on Table2 with Table1.PrimaryKey = Table2.ForeignKey. 我有两个表,如Table1和Table2,我左边加入表2的Table1,Table1.PrimaryKey = Table2.ForeignKey。 It does return all the rows from both tables but in some rows, due to no record in Table2 for the join condition, the PrimaryKey field value is missing for Table1. 它确实从两个表返回所有行但在某些行中,由于Table2中没有连接条件的记录,因此Table1缺少PrimaryKey字段值。 Here is my code 这是我的代码

$this->db->select('*');
$this->db->from('CI_Articles_Tbl');
$this->db->join('CI_Article_Images_Tbl',
    'CI_Articles_Tbl.roflArticle_ID=CI_Article_Images_Tbl.roflArticle_ID','left');
$this->db->group_by('CI_Articles_Tbl.roflArticle_ID');
$query = $this->db->get();
return $query->result_array();

What is the problem in my query and what is its possible solution. 我的查询中有什么问题以及它可能的解决方案是什么。 Any help will be highly appreciated. 任何帮助将受到高度赞赏。

You have select table.* 你有选择table.*

Try this code. 试试这个代码。

$this->db->select('CI_Articles_Tbl.*');

My Answer for my own question is this but let me first Thank "Lighter"! 我对自己的问题的回答是这个,但让我首先感谢“打火机”! His Code helped me corrected my query, 他的代码帮助我纠正了我的疑问,

Suppose you have Two Tables Table1 and Table2 and you want to LEFT JOIN Table1 on Table2 or in other sense, You want all rows from both tables Whether or not a reference matches or not matches in both tables Table1 and Table2. 假设您有两个表Table1和Table2,并且您希望在表2或其他意义上LEFT JOIN Table1,您希望两个表中的所有行是否匹配或不匹配表Table1和Table2。 For example you want like this 例如,你想要这样

       Table1.P1 = Table2.P1

What i was doing that i went ahead and selected all fields from Table1 and then straight away gave the join condition on Table2 which is Wrong because sometimes Table1.P1 will NOT have a match Table2.P1, in such a case, rows will come but the value Table1.P1 will be missing. 我正在做什么,我继续选择Table1中的所有字段,然后直接在Table2上给出了连接条件,这是错误的,因为有时Table1.P1将没有匹配Table2.P1,在这种情况下,行将会出现但是值Table1.P1将丢失。

What you have to do is to show specifically what fields you want from what tables EXCEPT THE field of Table2 that you are using in Join condition. 您需要做的是具体显示您想要从哪些表中选择哪些字段除了您在Join条件中使用的Table2字段。 Like this Table1.* Table2.* except Table2.P1 (NOTE: VERY IMPORTANT) 像这样的Table1。* Table2。*除了Table2.P1(注意:非常重要)

My Code is here for any reference purposes 我的代码出于任何参考目的

$this->db->select('CI_Articles_Tbl.*,CI_Article_Images_Tbl.roflArticleImage_ID,CI_Article_Images_Tbl.roflArticleImage_Ext,CI_Users_Tbl.*');
        $this->db->from('CI_Articles_Tbl','CI_Article_Images_Tbl','CI_Users_Tbl');
        $this->db->join('CI_Article_Images_Tbl','CI_Articles_Tbl.roflArticle_ID=CI_Article_Images_Tbl.roflArticle_ID','left outer');
        $this->db->join('CI_Users_Tbl','CI_Articles_Tbl.roflUser_ID=CI_Users_Tbl.roflUser_ID');
        $this->db->group_by('CI_Articles_Tbl.roflArticle_ID');
        $query = $this->db->get();
        return $query->result_array();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM