返回介绍

solution / 1300-1399 / 1341.Movie Rating / README_EN

发布于 2024-06-17 01:03:20 字数 3928 浏览 0 评论 0 收藏 0

1341. Movie Rating

中文文档

Description

Table: Movies

+---------------+---------+
| Column Name   | Type  |
+---------------+---------+
| movie_id    | int   |
| title     | varchar |
+---------------+---------+
movie_id is the primary key (column with unique values) for this table.
title is the name of the movie.

 

Table: Users

+---------------+---------+
| Column Name   | Type  |
+---------------+---------+
| user_id     | int   |
| name      | varchar |
+---------------+---------+
user_id is the primary key (column with unique values) for this table.

 

Table: MovieRating

+---------------+---------+
| Column Name   | Type  |
+---------------+---------+
| movie_id    | int   |
| user_id     | int   |
| rating    | int   |
| created_at  | date  |
+---------------+---------+
(movie_id, user_id) is the primary key (column with unique values) for this table.
This table contains the rating of a movie by a user in their review.
created_at is the user's review date. 

 

Write a solution to:

  • Find the name of the user who has rated the greatest number of movies. In case of a tie, return the lexicographically smaller user name.
  • Find the movie name with the highest average rating in February 2020. In case of a tie, return the lexicographically smaller movie name.

The result format is in the following example.

 

Example 1:

Input: 
Movies table:
+-------------+--------------+
| movie_id  |  title     |
+-------------+--------------+
| 1       | Avengers   |
| 2       | Frozen 2   |
| 3       | Joker    |
+-------------+--------------+
Users table:
+-------------+--------------+
| user_id   |  name    |
+-------------+--------------+
| 1       | Daniel     |
| 2       | Monica     |
| 3       | Maria    |
| 4       | James    |
+-------------+--------------+
MovieRating table:
+-------------+--------------+--------------+-------------+
| movie_id  | user_id    | rating     | created_at  |
+-------------+--------------+--------------+-------------+
| 1       | 1      | 3      | 2020-01-12  |
| 1       | 2      | 4      | 2020-02-11  |
| 1       | 3      | 2      | 2020-02-12  |
| 1       | 4      | 1      | 2020-01-01  |
| 2       | 1      | 5      | 2020-02-17  | 
| 2       | 2      | 2      | 2020-02-01  | 
| 2       | 3      | 2      | 2020-03-01  |
| 3       | 1      | 3      | 2020-02-22  | 
| 3       | 2      | 4      | 2020-02-25  | 
+-------------+--------------+--------------+-------------+
Output: 
+--------------+
| results    |
+--------------+
| Daniel     |
| Frozen 2   |
+--------------+
Explanation: 
Daniel and Monica have rated 3 movies ("Avengers", "Frozen 2" and "Joker") but Daniel is smaller lexicographically.
Frozen 2 and Joker have a rating average of 3.5 in February but Frozen 2 is smaller lexicographically.

Solutions

Solution 1

# Write your MySQL query statement below
(
  SELECT name AS results
  FROM
    Users
    JOIN MovieRating USING (user_id)
  GROUP BY user_id
  ORDER BY COUNT(1) DESC, name
  LIMIT 1
)
UNION ALL
(
  SELECT title
  FROM
    MovieRating
    JOIN Movies USING (movie_id)
  WHERE DATE_FORMAT(created_at, '%Y-%m') = '2020-02'
  GROUP BY movie_id
  ORDER BY AVG(rating) DESC, title
  LIMIT 1
);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文