【问题标题】:How can I display fields from two different tables using active record of left join in codeigniter?如何使用 codeigniter 中左连接的活动记录显示来自两个不同表的字段?
【发布时间】:2016-08-23 07:48:30
【问题描述】:
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();
}

我无法显示会员表中的字段。

【问题讨论】:

  • 使用 select('membership.membership_id,membership.other_names,notification.*')
  • @Kool-Mind 你的意思是查询字符串而不是上面的那个?
  • select() 也应该填写会员的列名尝试 select('notification.*,membership.*') 并让我看看它是否有效
  • 每个推荐评论或答案都说同样的 :)。我也用更长的代码来回答,以便于查看。这只是为了眼睛,但为大家加油:)
  • @jackskiee 对你有帮助吗?如果有任何其他建议,请告诉我

标签: php android mysql codeigniter


【解决方案1】:

试试这个

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

【讨论】:

    【解决方案2】:
    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。

    祝你好运,玩得开心编码:D

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多