SELECT * FROM movies INNER JOIN Boxoffice ON movies.Id = Boxoffice.Movie_id;
【联表】找到所有国际销售额比国内销售大的电影
1 2 3 4 5
SELECT * FROM movies INNER JOIN Boxoffice ON movies.Id = Boxoffice.Movie_id WHERE Boxoffice.International_sales > Boxoffice.Domestic_sales;
【联表】找出所有电影按市场占有率rating倒序排列
1 2 3 4 5
SELECT * FROM movies INNER JOIN Boxoffice ON movies.Id = Boxoffice.Movie_id ORDER BY Boxoffice.rating;
【联表】每部电影按国际销售额比较,排名最靠前的导演是谁,国际销量多少
1 2 3 4 5 6
SELECT movies.Director,Boxoffice.International_sales FROM movies INNER JOIN Boxoffice ON movies.Id = Boxoffice.Movie_id ORDER BY Boxoffice.International_sales DESC LIMIT 1;
7 Outer Join
外连接不同于内连接,外连接会保留不能匹配的行。
A 外连接 B:
LEFT JOIN:保留所有A的行
RIGHT JOIN:保留所有B的行
FULL JOIN:保留所有的行
1 2 3 4 5 6 7
SELECT column, another_column, … FROM mytable INNER/LEFT/RIGHT/FULL JOIN another_table ON mytable.id = another_table.matching_id WHERE condition(s) ORDERBY column, … ASC/DESC LIMIT num_limit OFFSET num_offset;
【复习】找到所有有雇员的办公室(buildings)名字
1 2 3
SELECT DISTINCT Building FROM employees WHERE Building NOT NULL;
【复习】找到所有办公室里的所有角色(包含没有雇员的),并做唯一输出(DISTINCT)
1
【难题】找到所有有雇员的办公室(buildings)和对应的容量
1
Mysql 子查询
查询一个字段的各种长度和长度对应的个数,并按长度的降序排序。
1 2
SELECT t1.len,count(*) AS'数量'FROM (SELECTLENGTH(cust_city) AS len FROM customers) t1 GROUPBY t1.len ORDERBY t1.len DESC;
select tag,CONCAT(avg_play_bai,'%') as avg_play_progress from ( select tag, ROUND( AVG(IF(TIMESTAMPDIFF(SECOND,start_time,end_time) >= b.duration,1, TIMESTAMPDIFF(SECOND,start_time,end_time)/b.duration) *100) ,2) as avg_play_bai from tb_user_video_log a leftjoin tb_video_info b on a.video_id = b.video_id groupby b.tag HAVING avg_play_bai >60 ORDERby avg_play_bai desc ) as t
3.每类视频近一个月的转发量/率
无最近一个月的条件
1 2 3 4 5 6 7 8 9
# 转发率:转发次数/观看次数
select tag,sum(a.if_retweet) as retweet_cut, round(sum(a.if_retweet)/count(*),3) as retweet_rate from tb_user_video_log a leftjoin tb_video_info b on a.video_id = b.video_id groupby b.tag orderby retweet_rate desc
最近一个月的表示:题目是这一类视频,而非每个视频
1
datediff((selectmax(start_time) from tb_user_video_log),date(start_time)) <30
结果
1 2 3 4 5 6 7 8
select tag,sum(a.if_retweet) as retweet_cut, round(sum(a.if_retweet)/count(*),3) as retweet_rate from tb_user_video_log a leftjoin tb_video_info b on a.video_id = b.video_id where datediff((selectmax(start_time) from tb_user_video_log),date(start_time)) <30 groupby b.tag orderby retweet_rate desc
4.每个创作者每月的涨粉率及截止当前的总粉丝量
case的用法:
1 2 3 4 5
CASE WHEN sex = ‘1’ THEN ‘男’ WHEN sex = ‘2’ THEN ‘女’ ELSE ‘其他’ END
则粉丝数计算
1 2 3 4 5 6
sum( casewhen if_follow=1then1 when if_follow=2then-1 else0 end )
<窗口函数> over (partitionby <用于分组的列名> orderby <用于排序的列名>)
例如:如下的sql求得了按照班级顺序排名,班级内按照成绩排名的效果
rank()是一个聚合函数,这里可以替换成其他的函数
1 2 3 4
select *, rank() over (partitionby 班级 orderby 成绩 desc) as ranking from 班级表
这里的题目是按照作者排名,作者的每月粉丝量按照月份排序的效果
1 2 3 4 5 6 7 8
sum(sum(casewhen if_follow=1then1 when if_follow=2then-1 else0 end)) over ( partitionby author orderbymonthasc )
最终结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
select author, date_format(start_time,'%Y-%m') asmonth, round(sum(casewhen if_follow=1then1 when if_follow=2then-1 else0end) /count(author),3) as fans_growth_rate, sum(sum(casewhen if_follow=1then1 when if_follow=2then-1 else0end)) over (partitionby author orderby date_format(start_time,'%Y-%m') ) as total_fans
from tb_user_video_log a leftjoin tb_video_info b on a.video_id = b.video_id whereyear(start_time)=2021 groupby author,month orderby author,total_fans
5.月总刷题数和日均刷题数
按照月进行汇总与排序。
1 2 3 4
select data_format(submit_time,"%Y%m") as submit_month from t groupby submit_month orderby submit_month
selectCOALESCE(date_format(submit_time,'%Y%m'),"2021汇总") as submit_month, count(*) as month_q_cnt , round(count(*) /day(last_day(submit_time)),3) as avg_day_q_cnt from practice_record whereyear(submit_time) =2021 groupby date_format(submit_time,'%Y%m') WITHROLLUP
6.每类试卷得分前3名
按类型划分,且一个学生可能会有多个考试记录
1
groupbytag,uid
求得前三名排序: 使用开窗函数 row_number() over()
1
row_number() over(partitionby tag orderbymax(score) desc,min(score) desc,uid desc)
row_number 可以标出对应的序列
partition by 将结果按照tag进行分组
order by 将结果进行排序,先最高分排序,最高分相同按照最低分排序,否则按照uid排序
最终每个取前3名
1 2 3 4 5 6 7 8 9 10
select tid,uid,ranking from ( select a.tag as tid,uid, row_number() over(partitionby a.tag orderbymax(b.score) desc,min(b.score) desc,b.uid desc) as ranking from examination_info a leftjoin exam_record b on a.exam_id = b.exam_id groupby a.tag,b.uid ) t where ranking <=3;
select date_formate(trans_time,"%Y%v") as week-n, count(*) as succ_count from order_trade where order_status ='SUCCESS'and transtime <'2022-07-10' groupby channel_id,date_formate(trans_time,"%Y%v");