SQL语句高级(九)——max和min

一、max和min函数
查询student表中最大和最小的sbirthday日期值:--注意这里最大和最小的含义!!!!!!!!!
mysql> select sbirthday from student order by sbirthday;
+---------------------+
| sbirthday           |
+---------------------+
| 1974-11-11 00:00:00 |
| 1974-12-01 00:00:00 |
| 1975-08-21 00:00:00 |
| 1976-02-11 00:00:00 |
| 1977-09-01 00:00:00 |
| 1977-10-11 00:00:00 |
+---------------------+
6 rows in set (0.00 sec)

mysql> select max(sbirthday) as '最大',min(sbirthday) as '最小' from student; 
+---------------------+---------------------+
| 最大                | 最小                |
+---------------------+---------------------+
| 1977-10-11 00:00:00 | 1974-11-11 00:00:00 |
+---------------------+---------------------+
1 row in set (0.07 sec)

二、子查询
查询男教师及其所上的课程:
mysql> select * from teacher where tsex='man';
+-----+-------+------+---------------------+------------+--------+
| tno | tname | tsex | tbirthday           | prof       | depart |
+-----+-------+------+---------------------+------------+--------+
| 804 | aaa   | man  | 1986-12-10 00:00:00 | fujiaoshou | cs     |
| 811 | ddd   | man  | 1984-05-05 00:00:00 | zhujiao    | dianzi |
| 825 | ccc   | man  | 1966-10-04 00:00:00 | zhujiao    | cs     |
+-----+-------+------+---------------------+------------+--------+
3 rows in set (0.16 sec)

mysql> select * from course where tno in (select tno from teacher where tsex='man');
+--------+----------+-----+
| cno    | cname    | tno |
+--------+----------+-----+
| 6-1166 | math     | 804 |
| 3-105  | csdaolun | 825 |
+--------+----------+-----+
2 rows in set (0.11 sec)

三、max函数与子查询
查询最高分同学的sno、cno和degree列:
mysql> select max(degree) from score;
+-------------+
| max(degree) |
+-------------+
|          98 |
+-------------+
1 row in set (0.00 sec)

mysql> select * from score where degree=(select max(degree) from score);
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 104 | 3-245 |     98 |
+-----+-------+--------+





猜你喜欢

转载自blog.csdn.net/qq_37150711/article/details/87308600