MySQL第三次作业

 一、建库

mysql> create mydb11_stu;

二、建表

mysql> create table student(id int(10) not null unique primary key,name varchar(20) not null,sex varchar(4),birth year,department varchar(20),address varchar(50));
 
mysql> create table score(id int(10) not null unique primary key auto_increment,stu_id int(10) not null,c_name varchar(20),grade int(10));

三、数据

mysql> insert student values(901,'张三丰','男',2002,'计算机系','北京市海定区');
 
mysql> insert student values(902,'周全有','男',2000,'中文系','北京市昌平区');
 
mysql> insert student values(903,'张思维','女',2003,'中文系','湖南省永州市');
 
mysql> insert student values(904,'李广昌','男',1999,'英语系','辽宁省皋新市');
 
mysql> insert student values(905,'王翰','男',2004,'英语系','福建省厦门市');
 
mysql> insert student values(906,'王心凌','女',1998,'计算机系','湖南省衡阳市');

mysql> insert into score values(null,901,'计算机',98);
 
mysql> insert into score values(null,901,'英语',80);
 
mysql> insert into score values(null,902,'计算机',65);
 
mysql> insert into score values(null,902,'中文',88);
 
mysql> insert into score values(null,903,'中文',95);
 
mysql> insert into score values(null,904,'计算机',70);
 
mysql> insert into score values(null,904,'英语',92);
 
mysql> insert into score values(null,905,'英语',94);
 
mysql> insert into score values(null,906,'计算机',49);
 
mysql> insert into score values(null,906,'英语',83);

四、查询

(1)

mysql> select * from student;
+-----+--------+------+-------+------------+--------------+
| id  | name   | sex  | birth | department | address      |
+-----+--------+------+-------+------------+--------------+
| 901 | 张三丰 | 男   |  2002 | 计算机系   | 北京市海定区 |
| 902 | 周全有 | 男   |  2000 | 中文系     | 北京市昌平区 |
| 903 | 张思维 | 女   |  2003 | 中文系     | 湖南省永州市 |
| 904 | 李广昌 | 男   |  1999 | 英语系     | 辽宁省皋新市 |
| 905 | 王翰   | 男   |  2004 | 英语系     | 福建省厦门市 |
| 906 | 王心凌 | 女   |  1998 | 计算机系   | 湖南省衡阳市 |
+-----+--------+------+-------+------------+--------------+
mysql> select * from score;
+----+--------+--------+-------+
| id | stu_id | c_name | grade |
+----+--------+--------+-------+
|  1 |    901 | 计算机 |    98 |
|  2 |    901 | 英语   |    80 |
|  3 |    902 | 计算机 |    65 |
|  4 |    902 | 中文   |    88 |
|  5 |    903 | 中文   |    95 |
|  6 |    904 | 计算机 |    70 |
|  7 |    904 | 英语   |    92 |
|  8 |    905 | 英语   |    94 |
|  9 |    906 | 计算机 |    49 |
| 10 |    906 | 英语   |    83 |
+----+--------+--------+-------+

(2)

mysql> select * from student limit 2,4;
+-----+--------+------+-------+------------+--------------+
| id  | name   | sex  | birth | department | address      |
+-----+--------+------+-------+------------+--------------+
| 903 | 张思维 | 女   |  2003 | 中文系     | 湖南省永州市 |
| 904 | 李广昌 | 男   |  1999 | 英语系     | 辽宁省皋新市 |
| 905 | 王翰   | 男   |  2004 | 英语系     | 福建省厦门市 |
| 906 | 王心凌 | 女   |  1998 | 计算机系   | 湖南省衡阳市 |
+-----+--------+------+-------+------------+--------------+

(3)

mysql> select * from student where department='计算机系' or department='英语系';

+-----+--------+------+-------+------------+--------------+
| id  | name   | sex  | birth | department | address      |
+-----+--------+------+-------+------------+--------------+
| 901 | 张三丰 | 男   |  2002 | 计算机系   | 北京市海定区 |
| 904 | 李广昌 | 男   |  1999 | 英语系     | 辽宁省皋新市 |
| 905 | 王翰   | 男   |  2004 | 英语系     | 福建省厦门市 |
| 906 | 王心凌 | 女   |  1998 | 计算机系   | 湖南省衡阳市 |
+-----+--------+------+-------+------------+--------------+

(4)

mysql> select * from student where year(now()) - birth < 22;
+-----+--------+------+-------+------------+--------------+
| id  | name   | sex  | birth | department | address      |
+-----+--------+------+-------+------------+--------------+
| 903 | 张思维 | 女   |  2003 | 中文系     | 湖南省永州市 |
| 905 | 王翰   | 男   |  2004 | 英语系     | 福建省厦门市 |
+-----+--------+------+-------+------------+--------------+

(5)

mysql> select department'学院',count(1)'该学院人数' from student
    -> group by department;
+----------+------------+
| 学院     | 该学院人数 |
+----------+------------+
| 计算机系 |          2 |
| 中文系   |          2 |
| 英语系   |          2 |
+----------+------------+

(6)

mysql> select c_name,max(grade) from score
    -> group by c_name;
 
+--------+------------+
| c_name | max(grade) |
+--------+------------+
| 计算机 |         98 |
| 英语   |         94 |
| 中文   |         95 |
+--------+------------+

(7)

mysql> select name,c_name,grade from student join score on student.id=score.stu_id
    -> where name='李广昌';
+--------+--------+-------+
| name   | c_name | grade |
+--------+--------+-------+
| 李广昌 | 计算机 |    70 |
| 李广昌 | 英语   |    92 |
+--------+--------+-------+

(8)

mysql> select student.*,c_name,grade from student join score on student.id=score.stu_id;
+-----+--------+------+-------+------------+--------------+--------+-------+
| id  | name   | sex  | birth | department | address      | c_name | grade |
+-----+--------+------+-------+------------+--------------+--------+-------+
| 901 | 张三丰 | 男   |  2002 | 计算机系   | 北京市海定区 | 计算机 |    98 |
| 901 | 张三丰 | 男   |  2002 | 计算机系   | 北京市海定区 | 英语   |    80 |
| 902 | 周全有 | 男   |  2000 | 中文系     | 北京市昌平区 | 计算机 |    65 |
| 902 | 周全有 | 男   |  2000 | 中文系     | 北京市昌平区 | 中文   |    88 |
| 903 | 张思维 | 女   |  2003 | 中文系     | 湖南省永州市 | 中文   |    95 |
| 904 | 李广昌 | 男   |  1999 | 英语系     | 辽宁省皋新市 | 计算机 |    70 |
| 904 | 李广昌 | 男   |  1999 | 英语系     | 辽宁省皋新市 | 英语   |    92 |
| 905 | 王翰   | 男   |  2004 | 英语系     | 福建省厦门市 | 英语   |    94 |
| 906 | 王心凌 | 女   |  1998 | 计算机系   | 湖南省衡阳市 | 计算机 |    49 |
| 906 | 王心凌 | 女   |  1998 | 计算机系   | 湖南省衡阳市 | 英语   |    83 |
+-----+--------+------+-------+------------+--------------+--------+-------+

(9)

mysql> select stu_id,name,sum(grade)'总分' from student join score on student.id=score.stu_id
    -> group by stu_id;
+--------+--------+------+
| stu_id | name   | 总分 |
+--------+--------+------+
|    901 | 张三丰 |  178 |
|    902 | 周全有 |  153 |
|    903 | 张思维 |   95 |
|    904 | 李广昌 |  162 |
|    905 | 王翰   |   94 |
|    906 | 王心凌 |  132 |
+--------+--------+------+

(10)

mysql> select c_name,format(avg(grade),2)'平均分' from score
    -> group by c_name;
+--------+--------+
| c_name | 平均分 |
+--------+--------+
| 计算机 | 70.50  |
| 英语   | 87.25  |
| 中文   | 91.50  |
+--------+--------+

(11)

mysql> select student.* ,c_name,grade from student join score on student.id=score.stu_id
    -> where c_name='计算机' and grade<95;
+-----+--------+------+-------+------------+--------------+--------+-------+
| id  | name   | sex  | birth | department | address      | c_name | grade |
+-----+--------+------+-------+------------+--------------+--------+-------+
| 902 | 周全有 | 男   |  2000 | 中文系     | 北京市昌平区 | 计算机 |    65 |
| 904 | 李广昌 | 男   |  1999 | 英语系     | 辽宁省皋新市 | 计算机 |    70 |
| 906 | 王心凌 | 女   |  1998 | 计算机系   | 湖南省衡阳市 | 计算机 |    49 |
+-----+--------+------+-------+------------+--------------+--------+-------+

(12)

mysql> select c_name,grade from score
    -> where c_name='计算机'
    -> order by grade desc;
+--------+-------+
| c_name | grade |
+--------+-------+
| 计算机 |    98 |
| 计算机 |    70 |
| 计算机 |    65 |
| 计算机 |    49 |
+--------+-------+

(13)

mysql> select distinct student.id,score.stu_id from student inner join score on student.id=score.stu_id;
+-----+--------+
| id  | stu_id |
+-----+--------+
| 901 |    901 |
| 902 |    902 |
| 903 |    903 |
| 904 |    904 |
| 905 |    905 |
| 906 |    906 |
+-----+--------+

(14)

mysql> select name,department,c_name,grade from student join score on student.id=score.stu_id
    -> where name like '张%' or name like '王%';
+--------+------------+--------+-------+
| name   | department | c_name | grade |
+--------+------------+--------+-------+
| 张三丰 | 计算机系   | 计算机 |    98 |
| 张三丰 | 计算机系   | 英语   |    80 |
| 张思维 | 中文系     | 中文   |    95 |
| 王翰   | 英语系     | 英语   |    94 |
| 王心凌 | 计算机系   | 计算机 |    49 |
| 王心凌 | 计算机系   | 英语   |    83 |
+--------+------------+--------+-------+

(15)

mysql> select name,year(now())-birth'年龄',department,c_name,grade from student join score on student.id=score.stu_id
    -> where address like '湖南%';
+--------+------+------------+--------+-------+
| name   | 年龄 | department | c_name | grade |
+--------+------+------------+--------+-------+
| 张思维 |   21 | 中文系     | 中文   |    95 |
| 王心凌 |   26 | 计算机系   | 计算机 |    49 |
| 王心凌 |   26 | 计算机系   | 英语   |    83 |
+--------+------+------------+--------+-------+

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值