-1
我正在使用Sakila DB在MySQL中练习我的技能。总结后得到组的前n行
我会创建一个名为rentals_customer_store_film_category的视图,它是客户,付款,租赁,电影和类别表的联合。 我现在想通过收入获得前5名电影。这意味着我会撒谎总结每个电影的所有收入,然后返回第一个5. 我尝试了下面的代码,但它不起作用。 我无法弄清楚它有什么问题 有什么帮助吗?
SELECT store_id, film_id, income
FROM
(SELECT film_id, store_id, sum(amount) as income,
@store_rank := IF(@current_store = store_id, @store_rank + 1, 1) AS store_rank,
@current_store := store_id
FROM rentals_customer_store_film_category
group by store_id, film_id
ORDER BY store_id, income DESC, film_id
) ranked
WHERE store_rank <= 5
下面的结果。正如你所看到的,它并不停止在每个商店的第五排。它显示存储的所有电影,而我想只有前5店:店面ID 2.
store_id film_id income
1 971 134.82
1 879 132.85
1 938 127.82
1 973 123.83
1 865 119.84
1 941 117.82
1 267 116.83
1 327 110.85
1 580 106.86
1 715 105.85
1 897 104.85
...
...
...
...
2 878 127.83
2 791 127.82
2 854 123.83
2 946 117.86
2 396 113.81
2 369 111.84
2 764 110.85
2 260 110.84
2 838 110.82
2 527 109.83
2 893 106.84
2 71 102.87
2 8 102.82
...
...
...
...
帮助我们为您提供帮助 - 请分享一些示例数据和您试图获得的结果。 – Mureinik
完成。对不起,以前没有做过。 – user3623123