如何通过多个表的顺序获取MySQL查询

时间:2018-01-22 08:17:15

标签: php mysql

如果不使用示例,很难解释。这就是我的MySQL数据库的样子:

表名:general_info

Movie_ID    Movie_Name
1           Iron Man
2           Superman
3           Batman

表名:cast

Movie_ID     Cast_Name              Cast_Rating
1            Robert Downey Jr.      90
1            Gwyneth Paltrow        45

2            Henry Cavill           75
2            Amy Adams              65

3            Christian Bale         90
3            Heath Ledger           95

表名:Production_Companies

Movie_ID       Production_name        Production_rating
1              Marvel                 95
1              Paramount              80

2              Legendary Pictures     65
2              DC Entertainment       75

3              Legendary Pictures     65

现在,我想要获取电影:

首先添加铁人三项顶级评级+铁人三项制作公司评级的评级,并对每部电影进行评分。

像:

Iron man = 90 + 95 = 190 (Robert + Marvel)
SUperman = 75 + 75 = 150 (Henry + DC)
Batman = 95 + 65 = 160 (Christian + Legendary Pictures)

按此顺序获取电影:

Ironman
Batman
Superman

现在,我可以使用纯MySQL执行此操作,还是需要使用PHP?我的数据库结构是对的吗?

2 个答案:

答案 0 :(得分:0)

如果我找到了正确的东西,那么你需要的就是这样:

SELECT gi.Movie_Name, ((Select Cast_Rating 
FROM Cast as C 
where C.Movie_ID=gi.Movie_ID 
ORDER BY Cast_Rating desc 
LIMIT 1)+(SELECT Production_rating
 FROM Production_companies as PC 
where PC.Movie_ID=gi.Movie_ID 
ORDER BY Production_rating desc  
LIMIT 1)) as SummaryRating
    FROM general_info as gi 
    ORDER BY SummaryRating desc

它实际上会计算并显示你所说的每一个

答案 1 :(得分:0)

set @rt:=0;
select movie_id, movie, cast_name as 'actor', cast_rating as 'rating', Production_name as 'company', Production_rating as 'prodrating', @rt:= ( cast_rating + Production_rating ) as 'sum' from(    
    select 
        c.`movie_id`,
        i.`movie_name` as `movie`,
        c.`Cast_Name`,
        c.`Cast_Rating`,
        p.`Production_name`,
        p.`Production_rating`
    from `cast` c
        left outer join `general_info` i on i.`movie_id`=c.`movie_id`
        join `production_companies` p on p.`movie_id`=c.`movie_id`
    order by `cast_rating` desc
) `sub`
group by `movie_id`
order by `sum` desc;

此输出

+----------+----------+-------------------+--------+--------------------+------------+-----+
| movie_id | movie    | actor             | rating | company            | prodrating | sum |
+----------+----------+-------------------+--------+--------------------+------------+-----+
|        1 | Iron Man | Robert Downey Jr. |     95 | Marvel             |  95        | 190 |
|        3 | Batman   | Heath Ledger      |     95 | Legendary Pictures |  65        | 160 |
|        2 | Superman | Henry Cavill      |     75 | DC Entertainment   |  75        | 150 |
+----------+----------+-------------------+--------+--------------------+------------+-----+

或者,不是使用set声明变量,而是可以像这样内联地执行

select 
    @rt:=0 as 'x', 
    `movie_id`, 
    `movie`, 
    `cast_name` as 'actor', 
    `cast_rating` as 'rating', 
    `production_name` as 'company', 
    `production_rating` as 'prodrating', 
    @rt:= ( `cast_rating` + `production_rating` ) as 'overall-rating' 
    from (  
        select 
            c.`movie_id`,
            i.`movie_name` as `movie`,
            c.`cast_name`,
            c.`cast_rating`,
            p.`production_name`,
            p.`production_rating`
        from `cast` c
            left outer join `general_info` i on i.`movie_id`=c.`movie_id`
            join `production_companies` p on p.`movie_id`=c.`movie_id`
        order by `cast_rating` desc
    ) `sub`
group by `movie_id`
order by `overall-rating` desc;