首页 > 解决方案 > codeigniter中的内部连接查询

问题描述

代码:

public function draft_post($idd)
{
    $this->db->select('*');
    $this->db->from('registration');
    $this->db->join('draft_registration', 'registration.user_id= draft_registration.user_id','INNER');
    $this->db->where('registration.user_id', $idd);
    $query = $this->db->get();
    $result = $query->result_array();
    return $result;
}

在这段代码中,我有两个表,即registration and draft_registration. 现在,我在这里做什么,我想在 Codeigniter 中运行内部连接。现在,当我点击这个查询时会发生phpmyadmin什么显示错误的数据,即我有两行draft_registration和一行在registration表中,但它总是显示两个错误的表,我的查询看起来像我打印时的样子,如下所述:

SELECT *
FROM `registration`
INNER JOIN `draft_registration` ON `registration`.`user_id`= `draft_registration`.`user_id`
WHERE `registration`.`user_id` = '20181121064044'

那么,我该如何解决这个问题呢?请帮我。

谢谢你

标签: phpmysqlcodeigniter

解决方案


使用下面的代码

public function draft_post($idd)
{
$this->db->select('registration.*,draft_registration.*');
$this->db->from('registration');
$this->db->join('draft_registration', 'registration.user_id= draft_registration.user_id');
$this->db->where('registration.user_id', $idd);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}

或者您可以与对象一起使用

public function draft_post($idd)
{
$this->db->select('a.*,b.*');
$this->db->from('registration a');
$this->db->join('draft_registration b', 'a.user_id= b.user_id');
$this->db->where('a.user_id', $idd);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}

推荐阅读