從聽歌流水中找到18-25歲用戶在2022年每個月播放次數(shù)top 3的周杰倫的歌曲。 流水表 play_log: 日期 (fdate) 用戶 ID (user_id) 歌曲 ID (song_id) 2022-01-08 10000 0 2022-01-16 10000 0 2022-01-20 10000 0 2022-01-25 10000 0 2022-01-02 10000 1 2022-01-12 10000 1 2022-01-13 10000 1 2022-01-14 10000 1 2022-01-10 10000 2 2022-01-11 10000 3 2022-01-16 10000 3 2022-01-11 10000 4 2022-01-27 10000 4 2022-02-05 10000 0 2022-02-19 10000 0 2022-02-07 10000 1 2022-02-27 10000 2 2022-02-25 10000 3 2022-02-03 10000 4 2022-02-16 10000 4 歌曲表song_info: 歌曲 ID (song_id) 歌曲名稱 (song_name) 歌手名稱 (singer_name) 0 明明就 周杰倫 1 說好的幸福呢 周杰倫 2 江南 林俊杰 3 大笨鐘 周杰倫 4 黑鍵 林俊杰 用戶表user_info: user_id age 10000 18 輸出: month ranking song_name play_pv 1 1 明明就 4 1 2 說好的幸福呢 4 1 3 大笨鐘 2 2 1 明明就 2 2 2 說好的幸福呢 1 2 3 大笨鐘 1
示例1
輸入
drop table if exists play_log;
create table `play_log` (
`fdate` date,
`user_id` int,
`song_id` int
);
insert into play_log(fdate, user_id, song_id)
values
('2022-01-08', 10000, 0),
('2022-01-16', 10000, 0),
('2022-01-20', 10000, 0),
('2022-01-25', 10000, 0),
('2022-01-02', 10000, 1),
('2022-01-12', 10000, 1),
('2022-01-13', 10000, 1),
('2022-01-14', 10000, 1),
('2022-01-10', 10000, 2),
('2022-01-11', 10000, 3),
('2022-01-16', 10000, 3),
('2022-01-11', 10000, 4),
('2022-01-27', 10000, 4),
('2022-02-05', 10000, 0),
('2022-02-19', 10000, 0),
('2022-02-07', 10000, 1),
('2022-02-27', 10000, 2),
('2022-02-25', 10000, 3),
('2022-02-03', 10000, 4),
('2022-02-16', 10000, 4);
drop table if exists song_info;
create table `song_info` (
`song_id` int,
`song_name` varchar(255),
`singer_name` varchar(255)
);
insert into song_info(song_id, song_name, singer_name)
values
(0, '明明就', '周杰倫'),
(1, '說好的幸福呢', '周杰倫'),
(2, '江南', '林俊杰'),
(3, '大笨鐘', '周杰倫'),
(4, '黑鍵', '林俊杰');
drop table if exists user_info;
create table `user_info` (
`user_id` int,
`age` int
);
insert into user_info(user_id, age)
values
(10000, 18)
輸出
month|ranking|song_name|play_pv
1|1|明明就|4
1|2|說好的幸福呢|4
1|3|大笨鐘|2
2|1|明明就|2
2|2|說好的幸福呢|1
2|3|大笨鐘|1
說明
1月被18-25歲用戶播放次數(shù)最高的三首歌為“明明就”、“說好的幸福呢”、“大笨鐘”,“明明就”和“說好的幸福呢”播放次數(shù)相同,排名先后由兩者的song_id先后順序決定。2月同理。
備注:
MySQL中,日期轉(zhuǎn)月份的函數(shù)為 month(),例:SELECT MONTH(‘2016-01-16') 返回 1。
加載中...