duancaoqin6683 2017-04-18 07:59 采纳率: 0%
浏览 24

如何使用3个表的连接计算列数据

I have three tables

tbl_product 
item_id | item_name

company_details
v_id | item_id | company_name

user_ratings 
r_id | rate | v_id 

I want to count the rate and also get the rate of the company. Here is my query

SELECT  company_details.v_id,
        company_details.company_name,
        COUNT(user_ratings.rate) as vote,
        user_ratings.rate,
        tbl_product.item_name
FROM    company_details
LEFT JOIN tbl_product ON tbl_product.item_id = company_details.item_id
LEFT JOIN user_ratings ON user_ratings.v_id = company_details.v_id
GROUP BY company_details.v_id, user_ratings.rate

This is Whts i am Getting after this query:

v_id  company_name           vote  rate  item_name  
1     The Oberoi Udaivilas   1     4     5 Star Hotels  
1     The Oberoi Udaivilas   1     5     5 Star Hotels  
2     The Taj Mahal Palace   2     5     4 Star Hotels  
3     Rambagh Palace         1     5     3 Star Hotels  
4     Taj Lake Palace        1     5     5 Star Hotels  
5     Windflower Hall        1     3     2 Star Hotels  
5     Windflower Hall        1     5     2 Star Hotels  
6     Leela Palace Kempinski 0     n     4 Star Hotels  
7     Umaid Bhawan Palace    0     n     4 Star Hotels  
8     Hotel Ratan Vilas      0     n     4 Star Hotels  
9     The Leela Palace       0     n     4 Star Hotels  
10    The Imperial Hotel     0     n     3 Star Hotels  

You can see vote column is not counting.

This is what I am expecting

v_id  company_name          vote  rate  item_name   
1     The Oberoi Udaivilas  2     5     5 Star Hotels   
2     The Taj Mahal Palace  2     5     4 Star Hotels   

But this query is not counting the rate from user_ratings table, because of I also want to get the rate, if I remove the user_ratings.rate from select clause, then this query works, but when I add the user_ratings.rate in the select clause, then this query is not counting the rates as(vote), and it is returning as one rows in every count.

  • 写回答

4条回答 默认 最新

  • dongmimeng5500 2017-04-18 08:27
    关注

    Seems to me that your query is only missing an aggregation on the rate column, and from the expected output I'd say it's a max. You also should fix your group by. Try this one

    SELECT  company_details.v_id,
            company_details.company_name,
            COUNT(user_ratings.rate) as vote,
            MAX(user_ratings.rate) as rate,
            tbl_product.item_name
    FROM    company_details
    LEFT JOIN tbl_product ON tbl_product.item_id = company_details.item_id
    LEFT JOIN user_ratings ON user_ratings.v_id = company_details.v_id
    GROUP BY company_details.v_id,
             company_details.company_name,
             tbl_product.item_name
    
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?