多表联合查询问题

来源:互联网 发布:淘宝立体3d服装拍摄 编辑:程序博客网 时间:2024/05/21 14:52

数据库有如下三个表,每个表各个字段类型都是varchar2;

table1

table2

table3

(1)请设计一条sql语句,查出理科班的学生人数。

(2)请设计一条sql查询语句,查出所有理科班别的学生中每门课程的学习人数。

 

解答:

(1)由给出的数据表信息,知道要通过table1和table2来求得

方法1:用in

select count(student_name) from table2 where class_name in (select class_name from table1 where class_type='like')

方法2:用join

select count(student_name) from table2 t2 inner join table1 t1 on t2.class_name=t1.class_name where t1.class_type='like';

(2) 这题三个表都要用到

要统计一个字段对应不同值的个数;用group by

select major , count(student_name) num from table3 where student_name in (select count(student_name) from table2 where class_name in (select class_name from table1 where class_type='like')) group by major;

 

原创粉丝点击