2015-11-02 22 views
0

我想通過平均rating.so搜索,我有一個兩個表第一個餐廳表和第二個用戶評級。搜索功能獲取餐館名單。 搜索查詢計算平均評級由那裏休息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

這是不尋常/不太可能,這是你想要存儲的東西,而不是計算'即時' – Strawberry

+0

@Strawberry:我的猜測也與你一致 –

+0

如何能'休息類別id'成爲' users'?同樣的美食。這是bonkers – Strawberry

回答

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) 

不要忘了tu每次有新的投票(觸發或編程)時更新它。

無論如何,也許你不需要存儲?特別是如果你想獲得過濾器的平均評分。 您可以採取第一個查詢,並添加您的過濾器。

+0

但當我觸發此update.and更新如何獲得餐廳ID WHERE rest_id = restaurant.id – user3172435

+0

更新將更新所有餐廳。 如果您需要特定的餐廳,請將restaurant.id替換爲您想要的ID。 –