drop table if exists tea_stu;drop table if exists teacher;drop table if exists student;create table teacher(teaID int primary key,name varchar(50),age int);create table student(stuID int primary key,name varchar(50),age int);create table tea_stu(teaID int references teacher(teaID),stuID int references student(stuID));delete from teacher;delete from student;delete from tea_stu;insert into teacher(teaID, name, age) values(1,'zxx',45), (2,'lhm',25), (3,'wzg',26) , (4,'tg',27);insert into student(stuID, name, age) values(1,'wy',11), (2,'dh',25) , (3,'ysq',26) , (4,'mxc',27);insert into tea_stu(teaID, stuID) values(1,1), (1,2), (1,3);insert into tea_stu(teaID, stuID) values(2,2), (2,3), (2,4);insert into tea_stu(teaID, stuID) values(3,3), (3,4), (3,1);insert into tea_stu(teaID, stuID) values(4,4), (4,1), (4,2) , (4,3);select * from teacher;select * from student;select * from tea_stu;/*1.显示的字段要有老师name, age 每个老师所带的学生人数 *2.只列出老师age为40以下学生age为12以上的记录 */ /*要会统计分组信息*/select teaid, count(*) from tea_stu group by teaid;+-------+----------+| teaid | count(*) |+-------+----------+| 1 | 3 || 2 | 3 || 3 | 3 || 4 | 4 |+-------+----------+select * from tea_stu,teacher where tea_stu.teaID=teacher.teaID;+-------+-------+-------+------+------+| teaID | stuID | teaID | name | age |+-------+-------+-------+------+------+| 1 | 1 | 1 | zxx | 45 || 1 | 2 | 1 | zxx | 45 || 1 | 3 | 1 | zxx | 45 || 2 | 2 | 2 | lhm | 25 || 2 | 3 | 2 | lhm | 25 || 2 | 4 | 2 | lhm | 25 || 3 | 3 | 3 | wzg | 26 || 3 | 4 | 3 | wzg | 26 || 3 | 1 | 3 | wzg | 26 || 4 | 4 | 4 | tg | 27 || 4 | 1 | 4 | tg | 27 || 4 | 2 | 4 | tg | 27 || 4 | 3 | 4 | tg | 27 |+-------+-------+-------+------+------+/*再接着去掉大于40的老师:*/select * from tea_stu,teacher where tea_stu.teaID=teacher.teaID and teacher.age<=40;+-------+-------+-------+------+------+| teaID | stuID | teaID | name | age |+-------+-------+-------+------+------+| 2 | 2 | 2 | lhm | 25 || 2 | 3 | 2 | lhm | 25 || 2 | 4 | 2 | lhm | 25 || 3 | 3 | 3 | wzg | 26 || 3 | 4 | 3 | wzg | 26 || 3 | 1 | 3 | wzg | 26 || 4 | 4 | 4 | tg | 27 || 4 | 1 | 4 | tg | 27 || 4 | 2 | 4 | tg | 27 || 4 | 3 | 4 | tg | 27 |+-------+-------+-------+------+------+/*再对上面的结果去掉小于12的学生*/select * from (select tea_stu.teaID, tea_stu.stuID, teacher.name, teacher.agefrom tea_stu,teacher where tea_stu.teaID=teacher.teaID and teacher.age<40) as t,student where t.stuid=student.stuid and student.age>12;+-------+-------+------+------+-------+------+------+| teaID | stuID | name | age | stuID | name | age |+-------+-------+------+------+-------+------+------+| 2 | 2 | lhm | 25 | 2 | dh | 25 || 4 | 2 | tg | 27 | 2 | dh | 25 || 2 | 3 | lhm | 25 | 3 | ysq | 26 || 3 | 3 | wzg | 26 | 3 | ysq | 26 || 4 | 3 | tg | 27 | 3 | ysq | 26 || 2 | 4 | lhm | 25 | 4 | mxc | 27 || 3 | 4 | wzg | 26 | 4 | mxc | 27 || 4 | 4 | tg | 27 | 4 | mxc | 27 |+-------+-------+------+------+-------+------+------+/*再对上面的结果进行统计,显示的是老师的id和组信息*/select t.teaID,count(*) from (select tea_stu.teaID, tea_stu.stuID, teacher.name, teacher.agefrom tea_stu,teacher where tea_stu.teaID=teacher.teaID and teacher.age<40) as t,student where t.stuid=student.stuid and student.age>12group by t.teaID;+-------+----------+| teaID | count(*) |+-------+----------+| 2 | 3 || 3 | 2 || 4 | 3 |+-------+----------+/*然后对上面的东西进行改写,改写成显示老师的名字*/select teacher.name,t2.c from(select t.teaID,count(*) c from (select tea_stu.teaID, tea_stu.stuID, teacher.name, teacher.age from tea_stu,teacher where tea_stu.teaID=teacher.teaID and teacher.age<40) as t,student where t.stuid=student.stuid and student.age>12group by t.teaID) as t2,teacherwhere teacher.teaID=t2.teaID;+------+---+| name | c |+------+---+| lhm | 3 || wzg | 2 || tg | 3 |+------+---+/*第二种写法:*/select teacher.teaID, teacher.name, t1.total from teacher,(select teaID,count(tea_stu.stuID) total from tea_stu, student where tea_stu.stuID = student.stuID and student.age>12group by teaID ) as t1where teacher.teaID = t1.teaID and teacher.age<40 ;
- 1.显示的字段要有老师name, age 每个老师所带的学生人数,2 只列出老师age为40以下学生age为12以上的记录
- 老师age为40以下,学生age为12以上的记录
- 【java】编写一个学生类,提供name,age,gender,phone,address,email成员变量, * 且为每个成员变量提供setter、getter方法。为学生类提供默认的构造器和带
- 【java】编写一个学生类,提供name,age,gender,phone,address,email成员变量, * 且为每个成员变量提供setter、getter方法。为学生类提供默认的构造器和带
- SQL求出小于45岁的各个老师所带的大于12岁的学生人数
- 根据从控制台输入的信息(name,age,或score),来给5个学生排序
- git命令行操作记录(以廖雪峰老师的为蓝本,只做复习)
- input等的data-name、data-age HTML5 自定义属性
- 没有龊老师-----基于本文描述的老师为参考.
- 1.a.txt 文本里面写有如下内容: age=25:name=张三 age=26:name=李四 。。。。 已知:age和name,是Person类的两个成员变量,请把这个文本里面的
- 编写一个应用程序绘制一个如下的操作菜单并实现功能(定义一个类学生表示学生,有成员变量姓名name和年龄age) ***************************** | 1. 增
- 王老师送学生的40句话
- 为张孝祥老师的离世表示哀悼
- 刚才你们为老师锤的这几下背
- 根据老师编号,统计其所授课程的平均成绩及学生人数。
- attr_writer :age attr_reader :name
- act one's age 别没大没小的
- Expires和max-age的区别
- jquery为js做的鼠标点击事件
- matlab 各种函数详解(收集)
- HTML5中的Canvas绘图操作(五)
- JNI编程之如何传递参数(二)——数组类型的传递
- 如何在win7下装ubuntu(硬盘版安装)
- 1.显示的字段要有老师name, age 每个老师所带的学生人数,2 只列出老师age为40以下学生age为12以上的记录
- 悟透JavaScript之初看原型 --- (1)
- BOOL,int,float,指针类型的变量a 与“零”的比较语句
- 挖挖Hive的代码(三)——生成MapReduce(中)
- 悟透JavaScript之初看原型 --- (2)
- 【hdu】 find the most comfortable road (并查集)
- 悟透JavaScript之初看原型 --- (3)
- 我本将心对明月
- C#中的多线程使用 -- Thread 类: 使用回调函数从一个线程中检索数据