32-36

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 |
±--------------±--------------+
可以看到最大年份和最小年份其实是反的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值