codeigniter中的php内部连接查询

xiozqbni  于 2021-06-18  发布在  Mysql
关注(0)|答案(3)|浏览(290)

代码:

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'

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

ahy6op9u

ahy6op9u1#

指定要选择的列。或者,如果要选择表的所有列,可以使用: SELECT registration.* 在列名上带反记号``

qnakjoqk

qnakjoqk2#

使用以下代码

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;
}
i7uq4tfw

i7uq4tfw3#

$this->db->select('*'); //This code get all rows from both table.If you want a particular row you mention the column name.

For example:
$this->db->select('registration.name,draft_registration.city,draft_registration.state');

相关问题