如何添加我计算平均评分并从我的专栏

时间:2015-11-02 07:37:08

标签: mysql codeigniter

我想搜索平均评分。所以,我有一个两桌第一餐桌和第二个用户评级.search功能,以获得餐厅列表。 搜索查询以通过其余ID计算平均评分,并将其添加到我的餐厅表格列中。

restaurant tabel
user_id | rest_name | rest_logo | fname| lname |user_type(2 customer 4 Restaurant owner)

rating
rate_id | user_id | rest_id |rate



 public function getRestaurantLists($post='',$position='', $item_par_page=''){

    if(!empty($post['rest_catid'])){
        $data2 = array(
            'rest_catid' => $post['rest_catid']
        );
        $this->session->set_userdata($data2);
    }
    if(!empty($post['cuisine'])){
        $data3 = array(
            'cuisine' => $post['cuisine']
        );
        $this->session->set_userdata($data3);
    }

    if(!empty($post['city'])){
        $data4 = array(
            'city' => $post['city']
        );
        $this->session->set_userdata($data4);
    }

    if(!empty($post['keyword'])){
        $data5 = array(
            'keyword' => $post['keyword']
        );
        $this->session->set_userdata($data5);
    }

     if(!empty($post['star'])){
        $data5 = array(
            'star' => $post['star']
        );
        $this->session->set_userdata($data5);
    }

    //echo $post['star'];
    $sql = '';

    $sql .= "SELECT  tbl_users.user_id, tbl_users.rest_name,tbl_rating.rate,tbl_users.rest_logo, tbl_users.address, tbl_users.city,
    tbl_rest_category.rest_category_name
    FROM tbl_users
    LEFT JOIN tbl_rest_category
    ON tbl_rest_category.rest_category_id = tbl_users.rest_category_id
    LEFT JOIN tbl_rest_cuisine
    ON tbl_rest_cuisine.rest_id = tbl_users.user_id 
    LEFT JOIN tbl_rating
    ON tbl_rating.rest_id = tbl_users.user_id   
    WHERE user_status = 1 AND user_type = 4 ";

    if($this->session->userdata('rest_catid')!== FALSE){
       $sql .= " AND tbl_users.rest_category_id = '".$this->session->userdata('rest_catid')."' ";
    }
    if($this->session->userdata('cuisine')!== FALSE){
       $sql .= " AND tbl_rest_cuisine.cuisine_id IN(".$this->session->userdata('cuisine').") ";
    }
    if($this->session->userdata('city')!== FALSE){
       $sql .= " AND tbl_users.city = '".$this->session->userdata('city')."' ";
    }
    if($this->session->userdata('keyword')!== FALSE){
       $sql .= " AND tbl_users.rest_name LIKE '%".$this->session->userdata('keyword')."%' ";
    }
     if($this->session->userdata('star')!== FALSE){
       $sql .= " AND tbl_rating.rate IN(".$this->session->userdata('star').") ";
    }
    $sql.=" GROUP BY tbl_users.user_id ";

    if(!empty($item_par_page)){
       $sql .= " LIMIT ".$position .",".$item_par_page;  
    }

    $query = $this->db->query($sql);    
   //echo $this->db->last_query();die;
    if($query->num_rows>0){
        return $query->result();
    }
}

1 个答案:

答案 0 :(得分:0)

要获得餐厅的平均评分:

SELECT AVG(rate) FROM rating WHERE rest_id = {rest_id}

要将它放在餐厅餐桌中,添加一个新列(假设为avg_rating,输入FLOAT)并且:

UPDATE restaurant
SET avg_rating = (SELECT AVG(rate) FROM rating WHERE rest_id = restaurant.id)

每次进行新的投票(触发或编程)时,不要忘记更新它。

无论如何,也许您不需要存储它?特别是如果您想通过过滤器获得平均评分。 您可以进行第一次查询,只需添加过滤器。