32查询所有任课 ( 在 course 表里有课程 ) 教师的 name 和 department 。
用到子查询
select name,department from teacher where no in(select t_no from course);
±-------±----------------+
| name | department |
±-------±----------------+
| 李诚 | 计算机系 |
| 王萍 | 计算机系 |
| 刘冰 | 电子工程系 |
| 张旭 | 电子工程系 |
±-------±----------------+
33、查出至少有2名男生的班号
解题思路:只查询性别为男, 然后按class进行分组,最后限制class的行大于等于2。
select class from student where sex=‘男' group by class
having count(class)>=2
34.查询student 表中 不姓"王"的同学的记录
select * from student where name not like '王%';
35、查询student 中每个学生的姓名和年龄
year(now())计算出当前年份
select name,year(now())-year(birthday) as age fromstudent;
36、查询 student 表中最大和最小的 birthday 值
select max(birthday) as min,min(birthday) as max from student;
如果按照这种方式写会出现逻辑错误
select max(birthday),min(birthday) from student;
得到结果如下:
±--------------±--------------+
| MAX(birthday) | MIN(birthday) |
±--------------±--------------+
| 1977-09-01 | 1974-06-03 |
±--------------±--------------+
可以看到最大年份和最小年份其实是反的。

被折叠的 条评论
为什么被折叠?



