【问题标题】:How can I join two tables with same fieldnames and return both in array using CodeIgniter?如何使用 CodeIgniter 连接两个具有相同字段名的表并在数组中返回两者?
【发布时间】:2018-04-04 14:16:03
【问题描述】:
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;
}

表结构如下:

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

输出数组从不包含来自 dev_db 的 id 和 name。它应该返回 software_db 中的所有字段,其中 software.id=$id 以及来自 dev_db 的 name 和 id。

【问题讨论】:

  • 您的意思是加入software_db.id=dev_db.id 而不是software_db.dev_id=dev_db.id
  • 选择不应该有 [ ]
  • @drfranks3 不,先生,它是 'software_db.dev_id=dev_db.id'

标签: php mysql codeigniter codeigniter-3


【解决方案1】:

select() 不应有方括号 [ ]

在您的选择子句中创建别名,例如

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

【讨论】:

    【解决方案2】:

    试试是这样的

    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();
    
    }
    

    【讨论】:

    • 它只返回dev_db的name字段
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 2011-05-17
    • 2018-10-11
    相关资源
    最近更新 更多