1

我有一个 sql 命令,它可以工作,但我不能在 Codeigniter 中使用它,

当我在 phpmyadmin 中使用它时,它是有效的

选择 online_visitors.page_id , hotels.hotel_name , count(online_visitors.page_id) as count_visit FROM online_visitors, hotelswhere online_visitors.page_type = 'hotel' AND online_visitors.is_bot = 0 AND hotels.ID = online_visitors.page_id group by online_visitors.page_id order by online_visitors。 page_id asc

但是当我想在codeigniter代码中使用它时,我不能

$this->db->select("page_id , hotel_name");
$this->db->from("hotels");
$this->db->join("online_visitors");
$this->db->where("is_bot", 0);
$this->db->where("page_type", 'hotel');
$this->db->group_by("page_id");
$this->db->order_by("page_id");
$this->db->get();
4

3 回答 3

2

您可以通过使用带有联接的 CodeIgniter 活动记录来尝试此操作

    $this->db->select('ov.page_id , h.hotel_name, count(ov.page_id) as count_visit ');
    $this->db->from('online_visitors as ov');
    $this->db->join('hotels as h','h.ID = ov.page_id');
    $this->db->where('ov.page_type','hotel');
    $this->db->where('ov.is_bot',0);
    $this->db->group_by('ov.page_id');
    $this->db->order_by('ov.page_id',"asc");
    $this->db->get();
于 2012-11-16T12:24:35.563 回答
1

您可以通过提到的 2 种方法来做到这一点

$query=$this->db->query(
'SELECT online_visitors.page_id , hotels.hotel_name , count(online_visitors.page_id) as count_visit FROM online_visitors , hotels WHERE online_visitors.page_type = 'hotel' AND online_visitors.is_bot = 0 AND hotels.ID = online_visitors.page_id group by online_visitors.page_id order by online_visitors.page_id asc'
);

你可以$query作为对象访问

或者您可以使用Active Record将其分解

于 2012-11-16T12:19:40.177 回答
0
$this->db->select("online_visitors.page_id , 
hotels.hotel_name,count(online_visitors.page_id) as count_visit");

$this->db>where("online_visitors.page_type = 'hotel'");
$this->db>where("online_visitors.is_bot = 0");
$this->db>where("hotels.ID = online_visitors.page_id");
$this->db->group_by("online_visitors.page_id");
$this->db->order_by("online_visitors.page_id", "asc");
于 2012-11-16T12:23:48.107 回答