lesson.sql 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #封装所有1,2,3级分类到视图中 包含:分类ID,分类名称 其他辅助字段
  2. create or replace view base_category_view as
  3. select bc3.id,
  4. bc1.id category1_id,
  5. bc1.name category1_name,
  6. bc2.id category2_id,
  7. bc2.name category2_name,
  8. bc3.id category3_id,
  9. bc3.name category3_name,
  10. bc3.create_time,
  11. bc3.update_time,
  12. bc3.is_deleted
  13. from base_category1 bc1
  14. inner join base_category2 bc2 on bc2.category1_id = bc1.id and bc2.is_deleted = 0
  15. inner join base_category3 bc3 on bc3.category2_id = bc2.id and bc3.is_deleted = 0
  16. where bc1.is_deleted = 0;
  17. #执行计划
  18. explain select bc3.id,
  19. bc1.id category1_id,
  20. bc1.name category1_name,
  21. bc2.id category2_id,
  22. bc2.name category2_name,
  23. bc3.id category3_id,
  24. bc3.name category3_name,
  25. bc3.create_time,
  26. bc3.update_time,
  27. bc3.is_deleted
  28. from base_category1 bc1
  29. inner join base_category2 bc2 on bc2.category1_id = bc1.id and bc2.is_deleted = 0
  30. inner join base_category3 bc3 on bc3.category2_id = bc2.id and bc3.is_deleted = 0
  31. where bc1.is_deleted = 0;
  32. #需求:根据1级分类ID查询该分类下关联标签及标签值列表
  33. select
  34. ba.id,
  35. ba.attribute_name,
  36. bav.id base_attribute_value_id,
  37. bav.attribute_id,
  38. bav.value_name
  39. from base_attribute ba inner join base_attribute_value bav
  40. on bav.attribute_id = ba.id and bav.is_deleted = 0
  41. where ba.category1_id = 2 and ba.is_deleted = 0;
  42. explain select
  43. ba.id,
  44. ba.attribute_name,
  45. bav.id base_attribute_value_id,
  46. bav.attribute_id,
  47. bav.value_name
  48. from base_attribute ba inner join base_attribute_value bav
  49. on bav.attribute_id = ba.id and bav.is_deleted = 0
  50. where ba.category1_id = 2 and ba.is_deleted = 0
  51. #需求:根据查询条件(用户ID、关键字、状态)查询专辑列表,包含:专辑信息以及专辑统计信息
  52. #第一步:查询业务数据来源专辑表跟专辑统计表 关联条件:统计表专辑ID跟专辑表主键 过滤条件:用户ID、关键字、状态 封装AlbumListVo
  53. select
  54. ai.id album_id,
  55. ai.album_title,
  56. ai.cover_url,
  57. ai.include_track_count,
  58. ai.is_finished,
  59. ai.status,
  60. stat.stat_type,
  61. stat.stat_num
  62. from album_info ai inner join album_stat stat on stat.album_id = ai.id;
  63. #第二步,上面得到统计是4条记录,需要封装成1条记录,封装到AlbumListVo中,封装字段:播放量、点赞量、收藏量、评论量 行转为列
  64. #2.1 尝试根据专辑ID分组 问题:违背SQL严格模式语法规则only_full_group_by,select中只能分组字段、聚合函数 由于查询列中包含多方统计类型,统计数值
  65. select
  66. ai.id album_id,
  67. ai.album_title,
  68. ai.cover_url,
  69. ai.include_track_count,
  70. ai.is_finished,
  71. ai.status,
  72. stat.stat_type,
  73. stat.stat_num
  74. from album_info ai inner join album_stat stat on stat.album_id = ai.id
  75. group by ai.id;
  76. #2.2 无法再Group by增加分组字段,从Select作为切入点 Select部分可以使用聚合函数
  77. # 采用判断函数 if(条件,满足结果,不满足结果)
  78. select if(1=2, 'true', 'false');
  79. # 以其中某条统计数值为例 得到统计类型及数值 转为列
  80. select * from album_stat where album_id = 2;
  81. select if(stat_type='0401', stat_num, 0) play_stat_num,if(stat_type='0402', stat_num, 0) subscribeStatNum from album_stat where album_id = 2;
  82. # 完善SQL 对统计每行记录进行判断
  83. explain select
  84. ai.id album_id,
  85. ai.album_title,
  86. ai.cover_url,
  87. ai.include_track_count,
  88. ai.is_finished,
  89. ai.status,
  90. max(if(stat.stat_type='0401', stat_num, 0)) playStatNum,
  91. max(if(stat.stat_type='0402', stat_num, 0)) subscribeStatNum,
  92. sum(if(stat.stat_type='0403', stat_num, 0)) buyStatNum,
  93. max(if(stat.stat_type='0404', stat_num, 0)) commentStatNum
  94. from album_info ai inner join album_stat stat on stat.album_id = ai.id
  95. group by ai.id;
  96. #第三步:增加过滤条件,排序
  97. explain select
  98. ai.id album_id,
  99. ai.album_title,
  100. ai.cover_url,
  101. ai.include_track_count,
  102. ai.is_finished,
  103. ai.status,
  104. max(if(stat.stat_type='0401', stat_num, 0)) playStatNum,
  105. max(if(stat.stat_type='0402', stat_num, 0)) subscribeStatNum,
  106. sum(if(stat.stat_type='0403', stat_num, 0)) buyStatNum,
  107. max(if(stat.stat_type='0404', stat_num, 0)) commentStatNum
  108. from album_info ai inner join album_stat stat on stat.album_id = ai.id
  109. where ai.user_id = ?
  110. and ai.album_title like concat('%','世界','%')
  111. and ai.status = ?
  112. and ai.is_deleted = 0
  113. group by ai.id
  114. order by ai.id desc;
  115. #需求:条件分页查询声音列表,包含声音统计信息 封装到TrackListVo对象中
  116. #第一步:关联查询声音表跟统计表 关联条件:声音表ID跟统计表声音ID
  117. select
  118. ti.id track_id,
  119. ti.track_title,
  120. ti.cover_url,
  121. ti.media_duration,
  122. ti.status,
  123. stat.stat_type,
  124. stat.stat_num
  125. from track_info ti inner join track_stat stat on stat.track_id = ti.id;
  126. #第二步:先根据声音ID分组,借助if函数获取某种统计类型对应数值(4个数值) + max函数获取有效统计数值
  127. select
  128. ti.id track_id,
  129. ti.track_title,
  130. ti.cover_url,
  131. ti.media_duration,
  132. ti.status,
  133. max(if(stat_type='0701', stat_num, 0)) playStatNum,
  134. max(if(stat_type='0702', stat_num, 0)) collectStatNum,
  135. max(if(stat_type='0703', stat_num, 0)) praiseStatNum,
  136. max(if(stat_type='0704', stat_num, 0)) commentStatNum
  137. from track_info ti inner join track_stat stat on stat.track_id = ti.id
  138. group by ti.id
  139. order by ti.id desc;
  140. #第三步过滤条件
  141. explain select
  142. ti.id track_id,
  143. ti.track_title,
  144. ti.cover_url,
  145. ti.media_duration,
  146. ti.status,
  147. max(if(stat_type='0701', stat_num, 0)) playStatNum,
  148. max(if(stat_type='0702', stat_num, 0)) collectStatNum,
  149. max(if(stat_type='0703', stat_num, 0)) praiseStatNum,
  150. max(if(stat_type='0704', stat_num, 0)) commentStatNum
  151. from track_info ti inner join track_stat stat on stat.track_id = ti.id and stat.is_deleted = 0
  152. where
  153. ti.user_id = 1
  154. and ti.status = '0501'
  155. and ti.track_title like concat('%','世界','%')
  156. and ti.is_deleted = 0
  157. group by ti.id
  158. order by ti.id desc;
  159. # 根据专辑ID查询专辑统计信息
  160. select * from album_stat where album_id = 1;
  161. explain select
  162. album_id,
  163. max(if(stat_type='0401', stat_num, 0)) playStatNum,
  164. max(if(stat_type='0402', stat_num, 0)) subscribeStatNum,
  165. sum(if(stat_type='0403', stat_num, 0)) buyStatNum,
  166. max(if(stat_type='0404', stat_num, 0)) commentStatNum
  167. from album_stat where album_id = 1 and is_deleted = 0;
  168. #需求:根据专辑ID,分页获取专辑下声音列表 声音统计信息
  169. explain select
  170. ti.id track_id,
  171. ti.track_title,
  172. ti.media_duration,
  173. ti.order_num,
  174. ti.create_time,
  175. max(if(stat_type='0701', stat_num, 0)) playStatNum,
  176. max(if(stat_type='0702', stat_num, 0)) collectStatNum,
  177. max(if(stat_type='0703', stat_num, 0)) praiseStatNum,
  178. max(if(stat_type='0704', stat_num, 0)) commentStatNum
  179. from track_info ti inner join track_stat ts on ts.track_id = ti.id and ts.is_deleted = 0
  180. where ti.album_id = 1 and ti.status = '0501' and ti.is_deleted = 0
  181. group by ti.id
  182. order by ti.id asc;