如何从第三个表格(与Max()相关联,加入)

时间:2018-01-23 11:32:22

标签: mysql

让我用下面的例子来解释这个问题:(很长但很容易理解)

这就是我的MySQL数据库的样子:

表名:general_info

Movie_ID    movie_title
1           Iron Man
2           Superman
3           Batman

表名:cast

Movie_ID     Cast_Name              
1            Robert Downey Jr.      
1            Gwyneth Paltrow        

2            Henry Cavill           
2            Amy Adams              

3            Christian Bale         
3            Heath Ledger   

表名production_companies

Movie_ID       Production_name        
1              Marvel                 
1              Paramount              

2              Legendary Pictures     
2              DC Entertainment       

3              Snycopy    

表名user_cast_preference

user_id     user_cast_name         user_cast_rating
1           Robert Downey Jr.      95
1           Gwyneth Paltrow        45 
1           Christian Bale         80
1           Heath Ledger           90

表名user_production_preference

user_id        user_production_name   user_production_rating
1              Marvel                 85
1              Paraamount             70
1              Syncopy                65

<小时/> <小时/>

现在,我可以使用此查询获取用户首选的演员+首选制作公司中的所有电影

select general_info.movie_id,movie_title from general_info

inner join cast on general_info.movie_id = cast.movie_id 
inner join production_companies on general_info.movie_id = production_companies.movie_id

where cast.cast_name in (select user_cast_name from user_cast_preference) 
or production_companies.production_companie_name in (select user_production_name from user_production_preference)

group by movie_title 

当前结果:

movie_id     moive_title

3            Batman
1            Iron Man

只有蝙蝠侠和铁人才被抓获,因为至少有一个演员或制作公司参与其中(这也是用户的首选名单)

直到现在一切都很好。但我想这样做:

我想通过这个algorithem订购电影。

我会将所有获取的电影与我表格中给定的评分进行比较,然后按照从上到下的顺序进行排序。

就我而言,让我们来比较蝙蝠侠和钢铁侠。

这是怎么回事,我决定比较一下。

Take the top rated cast from Ironman + Top rated production company from iron man

Take the top rated cast from Batman + Top rated production company from batman

即:

95 (iron man) + 85 (marvel) = 180
90 (heath ledger) + 65 (syncopy) = 155

现在钢铁侠的评分高于蝙蝠侠,所以预期结果将是:

movie_id     moive_title    total_rating

1            Iron Man       180
3            BatMan         155

我希望,我明白了。

2 个答案:

答案 0 :(得分:0)

不确定您在原始代码中获取“production_companies.production_companie_name”的位置

SELECT general_info.Movie_ID,
       general_info.movie_title,
       MAX(user_cast_preference.user_cast_rating + user_production_preference.user_production_rating) AS total_rating
FROM ((((general_info
         INNER JOIN CAST ON general_info.Movie_ID = cast.Movie_ID)
        INNER JOIN production_companies ON cast.Movie_ID = production_companies.Movie_ID)
       INNER JOIN user_production_preference ON production_companies.Production_name = user_production_preference.user_production_name)
      INNER JOIN user_cast_preference ON cast.Cast_Name = user_cast_preference.user_cast_name)
GROUP BY movie_title
ORDER BY total_rating DESC;

答案 1 :(得分:0)

您希望用户在其偏好设置中拥有演员或公司(或两者)的所有电影。然后你想按最佳偏好的总和进行排序。

我首先查看每个电影的用户评级演员和用户评级公司的最高分,然后将这些汇总结果外部加入电影表:

select gi.*
from general_info
left join
(
  select c.movie_id, max(ucp.user_cast_rating) as points
  from cast c
  join user_cast_preference ucp on ucp.user_cast_name = c.cast_name
  where ucp.user_id = 1
  group by c.movie_id
) p1 on p1.movie_id = gi.movie_id
left join
(
  select pc.movie_id, max(upp.user_cast_rating) as points
  from production_companies pc
  join user_production_preference upp on upp.user_cast_name = pc.cast_name
  where upp.user_id = 1
  group by pc.movie_id
) p2 on p1.movie_id = gi.movie_id
where p1.points > 0 or p2.points > 0
order by coalesce(p1.points, 0) + coalesce(p2.points, 0) desc;