简体   繁体   English

如何使用 CodeIgniter 连接两个具有相同字段名的表并在数组中返回它们?

[英]How can I join two tables with same fieldnames and return both in array using CodeIgniter?

public function appProfile($id=null)
{
   $query= $this->db->where('software_db.id',$id)
                    ->from('software_db')
                    ->join('dev_db','software_db.dev_id=dev_db.id','right')
                    ->select(['dev_db.name','dev_db.id','software_db.id','software_db.name','software_db.file_name','software_db.image_name'])
                    ->get()
                    ->result_array();
    print_r($query);die;
}

The table structure is as follows:表结构如下:

dev_db(id(primary key),name,email,password,comany,skills)
software_db(id(primary key), name,file_name,image_name,description,platform,cateogory)

The output array never contains the id and name from dev_db.输出数组从不包含来自 dev_db 的 id 和 name。 It should return all the fields from software_db where software.id=$id and name and id from dev_db.它应该返回来自 software_db 的所有字段,其中 software.id=$id 以及来自 dev_db 的 name 和 id。

Try is this way试试是这种方式

public function appProfile($id = '') {

    $this->db->select('dev_db.name, dev_db.id, software_db.id, software_db.name, software_db.file_name, software_db.image_name');
    $this->db->from('software_db');
    $this->db->join('dev_db', 'dev_db.id = software_db.dev_id', 'right');
    $this->db->where('software_db.id',$id);
    $query = $this->db->get();

    return $query->result_array();

}

select() shouldn't have square brackets [ ] . select()不应该有方括号[ ]

in your select clause create aliases, something like在您的 select 子句中创建别名,例如

->select('dev_db.name as dev_db_name') // etc

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

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