簡體   English   中英

如何使用codeigniter中的左連接的活動記錄顯示來自兩個不同表的字段?

[英]How can I display fields from two different tables using active record of left join in codeigniter?

function get_all_notification() { //get all notification based on the 'notification' table left join by 'membership' table

    $this->db->select()->from('notification')->join('membership','membership.membership_id = notification.notif_id','left');            $notif = $this->db->get();
    return $notif->result();
}

我無法顯示會員表中的字段。

嘗試這個

function get_all_notification() { //get all notification based on the 'notification' table left join by 'membership' table

    $this->db->select('*')->from('notification')->join('membership','membership.membership_id = notification.notif_id','left');
    $notif = $this->db->get();
    return $notif->result();
}
function get_all_notification()
{ 
    $this->db->select('n.*,m.*');
    $this->db->from('notification as n')
    $this->db->join('membership as m','m.membership_id = n.notif_id','left');
    $notif = $this->db->get();
    return $notif->result();
}

當您想要一個從上到下的干凈代碼時,可以使用此方法。 但是使用你的代碼可以很好地適應箭頭的連續性。 您只是忘了確定要選擇的字段。 使用別名也可以很好地選擇特定字段。

正如你所看到的,我使用了n。*和m。*,這只是一個通配符來從n表和m中選擇所有。 你可以指定你想要的東西n.your_field和m.your_field。

Goodluck並有樂趣編碼 :D

暫無
暫無

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

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