鱼C论坛

 找回密码
 立即注册
查看: 1586|回复: 0

MYSQL错题整理(需要掌握且易错的)

[复制链接]
发表于 2020-3-27 11:05:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 糖逗 于 2020-7-2 16:23 编辑

题目来源:leetcode
题目描述:
  1. 学生表: Students

  2. +---------------+---------+
  3. | Column Name   | Type    |
  4. +---------------+---------+
  5. | student_id    | int     |
  6. | student_name  | varchar |
  7. +---------------+---------+
  8. 主键为 student_id(学生ID),该表内的每一行都记录有学校一名学生的信息。
  9.  

  10. 科目表: Subjects

  11. +--------------+---------+
  12. | Column Name  | Type    |
  13. +--------------+---------+
  14. | subject_name | varchar |
  15. +--------------+---------+
  16. 主键为 subject_name(科目名称),每一行记录学校的一门科目名称。
  17.  

  18. 考试表: Examinations

  19. +--------------+---------+
  20. | Column Name  | Type    |
  21. +--------------+---------+
  22. | student_id   | int     |
  23. | subject_name | varchar |
  24. +--------------+---------+
  25. 这张表压根没有主键,可能会有重复行。
  26. 学生表里的一个学生修读科目表里的每一门科目,而这张考试表的每一行记录就表示学生表里的某个学生参加了一次科目表里某门科目的测试。
  27.  

  28. 要求写一段 SQL 语句,查询出每个学生参加每一门科目测试的次数,结果按 student_id 和 subject_name 排序。

  29. 查询结构格式如下所示:

  30. Students table:
  31. +------------+--------------+
  32. | student_id | student_name |
  33. +------------+--------------+
  34. | 1          | Alice        |
  35. | 2          | Bob          |
  36. | 13         | John         |
  37. | 6          | Alex         |
  38. +------------+--------------+
  39. Subjects table:
  40. +--------------+
  41. | subject_name |
  42. +--------------+
  43. | Math         |
  44. | Physics      |
  45. | Programming  |
  46. +--------------+
  47. Examinations table:
  48. +------------+--------------+
  49. | student_id | subject_name |
  50. +------------+--------------+
  51. | 1          | Math         |
  52. | 1          | Physics      |
  53. | 1          | Programming  |
  54. | 2          | Programming  |
  55. | 1          | Physics      |
  56. | 1          | Math         |
  57. | 13         | Math         |
  58. | 13         | Programming  |
  59. | 13         | Physics      |
  60. | 2          | Math         |
  61. | 1          | Math         |
  62. +------------+--------------+
  63. Result table:
  64. +------------+--------------+--------------+----------------+
  65. | student_id | student_name | subject_name | attended_exams |
  66. +------------+--------------+--------------+----------------+
  67. | 1          | Alice        | Math         | 3              |
  68. | 1          | Alice        | Physics      | 2              |
  69. | 1          | Alice        | Programming  | 1              |
  70. | 2          | Bob          | Math         | 1              |
  71. | 2          | Bob          | Physics      | 0              |
  72. | 2          | Bob          | Programming  | 1              |
  73. | 6          | Alex         | Math         | 0              |
  74. | 6          | Alex         | Physics      | 0              |
  75. | 6          | Alex         | Programming  | 0              |
  76. | 13         | John         | Math         | 1              |
  77. | 13         | John         | Physics      | 1              |
  78. | 13         | John         | Programming  | 1              |
  79. +------------+--------------+--------------+----------------+
  80. 结果表需包含所有学生和所有科目(即便测试次数为0):
  81. Alice 参加了 3 次数学测试, 2 次物理测试,以及 1 次编程测试;
  82. Bob 参加了 1 次数学测试, 1 次编程测试,没有参加物理测试;
  83. Alex 啥测试都没参加;
  84. John  参加了数学、物理、编程测试各 1 次。
复制代码

  1. select a.student_id,a.student_name,a.subject_name,
  2. count(e.student_id) as attended_exams
  3. from
  4. (select *
  5. from students,subjects) as a
  6. left join Examinations as e
  7. on e.student_id=a.student_id and e.subject_name=a.subject_name
  8. group by a.student_name,a.subject_name
  9. order by a.student_id,a.subject_name
复制代码


注意事项:
1.这里用到笛卡尔乘积

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 12:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表