簡體   English   中英

Codeigniter中的內部聯接查詢

[英]Inner join query in 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'

那么,如何解決此問題? 請幫我。

謝謝

$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');

指定要選擇的列。 或者,如果要選擇表的所有列,則可以使用:

SELECT registration.*在列名上帶有反引號``

使用以下代碼

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM