Codeigniter活动记录加入2 x来自语句

时间:2014-02-25 09:01:36

标签: php mysql sql codeigniter activerecord

您已经编写了下面的代码,这是我认为一个带连接的简单选择。

    $this->db->select('PAS_User.user_first_name, PAS_User.user_last_name, PAS_User.user_avatar_url, AMS_Notification.noti_entitiy_id, AMS_Notification.noti_entity_type, AMS_Notification.noti_updated FROM AMS_Notification INNER JOIN PAS_User ON AMS_Notification.noti_from_user_id = PAS_User.user_user_id');
    $this->db->from('AMS_Notification');
    $this->db->join('PAS_User', 'PAS_User ON AMS_Notification.noti_from_user_id = PAS_User.user_user_id');
    $this->db->where('AMS_Notification.noti_to_user_id', $userid);
    $this->db->order_by("noti_updated", "desc");

    $query = $this->db->get('AMS_Notification');

    if($query -> num_rows() == 1) {
        return $query->result();
    } else {
        return false;
    }

然而,当我调用页面时,它显示了两个来自雄蕊的查询,任何人都可以告诉我这里做错了吗?

SELECT `PAS_User`.`user_first_name`, `PAS_User`.`user_last_name`, `PAS_User`.`user_avatar_url`, `AMS_Notification`.`noti_entitiy_id`, `AMS_Notification`.`noti_entity_type`, `AMS_Notification`.`noti_updated` FROM AMS_Notification INNER JOIN PAS_User ON AMS_Notification.noti_from_user_id = PAS_User.user_user_id FROM (`AMS_Notification`, `AMS_Notification`) JOIN `PAS_User` ON `PAS_User` `ON` AMS_Notification.noti_from_user_id = PAS_User.user_user_id WHERE `AMS_Notification`.`noti_to_user_id` = '7' ORDER BY `noti_updated` desc

1 个答案:

答案 0 :(得分:1)

$this->db->select('PAS_User.user_first_name, PAS_User.user_last_name, PAS_User.user_avatar_url, AMS_Notification.noti_entitiy_id, AMS_Notification.noti_entity_type, AMS_Notification.noti_updated');
$this->db->from('AMS_Notification');
$this->db->join('PAS_User', 'AMS_Notification.noti_from_user_id = PAS_User.user_user_id');
$this->db->where('AMS_Notification.noti_to_user_id', $userid);
$this->db->order_by("noti_updated", "desc");

$query = $this->db->get();

if($query -> num_rows() == 1) {
    return $query->result();
} else {
    return false;
}

无需在选择部分

中指定表名和JOIN