資料庫小練習
阿新 • • 發佈:2018-11-25
Q1:20120007, SELECT stu_name,stu_gender,stu_birthday FROM t_student WHERE stu_id = 20127007 Q2:會計1201,find student"s id & name SELECT a.stu_id,a.stu_name FROM t_student as a INNER JOIN t_class b ON b.cls_id = a.cls_id WHERE b.cls_name = '會計1201' Q3:會計,2012,find classes with students' couunt SELECT count(b.stu_id) FROM t_class a inner join t_student b ON b.cls_id = a.cls_id WHERE a.grade = 2012 AND a.subject= '會計' 第二種 SELECT a.cls_name, count(*) FROM t_class a INNER JOIN t_student b ON a.cls_id = b.cls_id WHERE a.subject = '會計' AND a.grade = 2012 GROUP BY a.cls_name Q4:20127007 find all failured course SELECT c.course_name FROM t_mark a INNER JOIN t_course c ON c.course_id = a.course_id WHERE a.stu_id = 20127001 AND a.scroll < 60 Q5:Add a new course,17317,'Course Foundation',3.5 INSERT INTO t_course ( course_id,course_name,course_credit) VALUES (17317,'DB Foundation',3.5) Q6:會計,2012,英語[1],FIND avg SELECT DISTINCT avg(c.scoll) FROM t_class a inner join t_student b ON b.cls_id = a.cls_id inner join t_mark c ON b.stu_id = c.stu_id inner join t_course d ON d.course_id = c.course_id WHERE a.grade = 2012 AND a.subject = '會計' AND d.course_name = '英語[1]' Q7:會計,2012,英語[1],failure students SELECT DISTINCT b.* FROM t_class a inner join t_student b ON b.cls_id = a.cls_id inner join t_mark c ON b.stu_id = c.stu_id inner join t_course d ON d.course_id = c.course_id WHERE c.scroll < 60 AND a.subject = '會計' AND d.course_name = '英語[1]' 、方法2 SELECT a.stu_id,a.stu_name FROM t_student a INNER JOIN t_class b ON a.cls_id = b.cls_id WHERE b.subject = '會計' AND b.grade = 2012 EXISTS( SELECT 1 FROM t_mark c INNER JOIN t_course d ON d.course_id = c.course_id] WHERE c.stu_id = a.stu_id AND c.scroll < 60 AND d.course_name = '英語[1]' ) Q8:會計,2012,5-course-failure SELECT DISTINCT b.stu_name/*按分組的列顯示*/ FROM t_class a inner join t_student b ON b.cls_id = a.cls_id inner join t_mark c ON b.stu_id = c.stu_id inner join t_course d ON d.course_id = c.course_id WHERE c.scroll < 60 AND a.subject = '會計' AND a.grade =2012 GROUP BY b.stu_name HAVING count(distinct c.course_id) >=5 方法2 SELECT DISTINCT b.stu_name,count(*),count(c.course_id)/*按分組的列顯示*/ FROM t_class a inner join t_student b ON b.cls_id = a.cls_id inner join t_mark c ON b.stu_id = c.stu_id inner join t_course d ON d.course_id = c.course_id WHERE c.scroll < 60 AND a.subject = '會計' AND a.grade =2012 GROUP BY b.stu_name HAVING count(distinct c.course_id) >=5