当前位置:   article > 正文

mysql 语句练习笔记_mysql having round

mysql having round
  1. -- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
  2. SELECT a.*,b.s_score as '01-课程',c.s_score as '02-课程'
  3. FROM student a
  4. JOIN score b on a.s_id = b.s_id AND b.c_id = '01'
  5. LEFT JOIN score c on a.s_id = c.s_id AND c.c_id='02' OR c.c_id=NULL
  6. WHERE b.s_score > c.s_score
  7. -- 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
  8. SELECT a.*,b.s_score as '01',c.s_score AS '02'
  9. FROM student a
  10. JOIN score b on a.s_id = b.s_id AND b.c_id = '01'
  11. left JOIN score c on a.s_id = c.s_id AND c.c_id = '02'
  12. where b.s_score<c.s_score
  13. -- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
  14. SELECT a.s_id AS '学号',a.s_name AS '名字',ROUND(AVG(b.s_score),2) AS '大于60平均成绩'
  15. FROM student a
  16. JOIN score b on a.s_id = b.s_id
  17. GROUP BY a.s_name,a.s_id
  18. HAVING ROUND(AVG(b.s_score),2) >= 60
  19. ORDER BY a.s_id desc
  20. -- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩-- (包括有成绩的和无成绩的)
  21. SELECT a.s_id AS '学号',a.s_name AS '名字',ROUND(AVG(b.s_score),2) AS '小于等于60平均成绩'
  22. FROM student a
  23. JOIN score b on a.s_id = b.s_id
  24. GROUP BY a.s_name,a.s_id
  25. HAVING ROUND(AVG(b.s_score),2) <= 60 UNION
  26. SELECT b.s_id AS '学号',b.s_name AS '名字',0 AS '小于等于60平均成绩'
  27. FROM student b
  28. WHERE b.s_id not in ( -- 获取没有成绩的人,并且将对应的
  29. SELECT distinct s_id
  30. FROM score
  31. )
  32. -- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
  33. SELECT a.s_id AS '学生编号',a.s_name AS "学生姓名",COUNT(b.c_id) AS '选课总数',SUM(b.s_score) AS '总成绩'
  34. FROM student a
  35. JOIN score b on a.s_id = b.s_id
  36. GROUP BY a.s_id,a.s_name
  37. -- 6、查询"李"姓老师的数量
  38. SELECT COUNT(t_id)
  39. FROM teacher
  40. WHERE t_name LIKE '李%'
  41. -- 7、查询学过"张三"老师授课的同学的信息
  42. SELECT a.*
  43. FROM student a
  44. JOIN score b on a.s_id = b.s_id
  45. WHERE b.c_id in (
  46. SELECT c_id
  47. FROM course
  48. WHERE t_id =(
  49. SELECT t_id
  50. FROM teacher
  51. WHERE t_name = '张三'
  52. ))
  53. -- 8、查询没学过"张三"老师授课的同学的信息
  54. SELECT *
  55. FROM student s
  56. WHERE s.s_id not in (
  57. SELECT a.s_id
  58. FROM student a
  59. JOIN score b on a.s_id = b.s_id
  60. WHERE b.c_id IN(
  61. SELECT c_id
  62. FROM course
  63. WHERE t_id = (
  64. SELECT t_id
  65. FROM teacher
  66. WHERE t_name = '张三'
  67. )
  68. )
  69. )
  70. -- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
  71. SELECT a.*
  72. FROM student a,score b,score c
  73. WHERE a.s_id = b.s_id AND a.s_id = c.s_id AND b.c_id = '01' AND c.c_id = '02'
  74. -- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
  75. SELECT a.*
  76. FROM student a
  77. WHERE a.s_id in (
  78. SELECT s_id
  79. FROM score
  80. WHERE c_id = '01'
  81. )
  82. AND a.s_id not in (
  83. SELECT s_id
  84. FROM score
  85. WHERE c_id = '02'
  86. )
  87. -- 11、查询没有学全所有课程的同学的信息
  88. select s.*
  89. from student s
  90. where s.s_id in(
  91. select s_id from score where s_id not in(
  92. select a.s_id
  93. from score a
  94. join score b on a.s_id = b.s_id and b.c_id='02'
  95. join score c on a.s_id = c.s_id and c.c_id='03'
  96. where a.c_id='01'))
  97. -- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
  98. SELECT s.*
  99. FROM student s
  100. WHERE s.s_id in(
  101. SELECT DISTINCT s_id
  102. FROM score
  103. WHERE c_id in (
  104. SELECT a.c_id
  105. F
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号