创建数据库和使用数据库
mysql> create database mydb11_stu;
Query OK, 1 row affected (0.03 sec)
mysql> use mydb11_stu;
Database changed
创建表及导入相关数据
mysql> create table student (id int (10) primary key, name varchar(10) , sex varchar(4) , birth_year year, department varchar(20),address varchar(50));
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql> create table score (id int(10) primary key auto_increment, stu_id int(10) not null,c_name varchar(20),grade int(10));
Query OK, 0 rows affected, 3 warnings (0.02 sec)
mysql> insert into student values (901, '张三丰', '男', 2002, '计算机系', '北京市海淀区');
Query OK, 1 row affected (0.01 sec)
mysql> insert into student values (902, '周全有', '男', 2000, '中文系', '北京市昌平区');
Query OK, 1 row affected (0.00 sec)
mysql> insert into student values (903, '张思维', '女', 2003, '中文系', '湖南省永州市');
Query OK, 1 row affected (0.00 sec)
mysql> insert into student values (904, '李广昌', '男', 1999, '英语系', '辽宁省阜新市');
Query OK, 1 row affected (0.00 sec)
mysql> insert into student values (905, '王翰', '男', 2004, '英语系', '福建省厦门市');
Query OK, 1 row affected (0.00 sec)
mysql> insert into student values (906, '王心凌', '女', 1998, '计算机系', '湖南省衡阳市');
Query OK, 1 row affected (0.00 sec)
mysql> insert into score values
-> (null, 901, '计算机', 98),
-> (null, 901, '英语', 80),
-> (null, 902, '计算机', 65),
-> (null, 902, '中文', 88),
-> (null, 903, '中文', 95),
-> (null, 904, '计算机', 70),
-> (null, 904, '英语', 92),
-> (null, 905, '英语', 94),
-> (null, 906, '计算机', 49),
-> (null, 906, '英语', 83);
Query OK, 10 rows affected (0.01 sec)
Records: 10 Duplicates: 0 Warnings: 0
实现查询
mysql> select * from student;
+-----+--------+------+------------+------------+--------------+
| id | name | sex | birth_year | department | address |
+-----+--------+------+------------+------------+--------------+
| 901 | 张三丰 | 男 | 2002 | 计算机系 | 北京市海淀区 |
| 902 | 周全有 | 男 | 2000 | 中文系 | 北京市昌平区 |
| 903 | 张思维 | 女 | 2003 | 中文系 | 湖南省永州市 |
| 904 | 李广昌 | 男 | 1999 | 英语系 | 辽宁省阜新市 |
| 905 | 王翰 | 男 | 2004 | 英语系 | 福建省厦门市 |
| 906 | 王心凌 | 女 | 1998 | 计算机系 | 湖南省衡阳市 |
+-----+--------+------+------------+------------+--------------+
6 rows in set (0.01 sec)
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 |
+----+--------+--------+-------+
10 rows in set (0.00 sec)
mysql> select * from student limit 1,4;
+-----+--------+------+------------+------------+--------------+
| id | name | sex | birth_year | department | address |
+-----+--------+------+------------+------------+--------------+
| 902 | 周全有 | 男 | 2000 | 中文系 | 北京市昌平区 |
| 903 | 张思维 | 女 | 2003 | 中文系 | 湖南省永州市 |
| 904 | 李广昌 | 男 | 1999 | 英语系 | 辽宁省阜新市 |
| 905 | 王翰 | 男 | 2004 | 英语系 | 福建省厦门市 |
+-----+--------+------+------------+------------+--------------+
4 rows in set (0.00 sec)
mysql> select * from student limit 1 offset 4;
+-----+------+------+------------+------------+--------------+
| id | name | sex | birth_year | department | address |
+-----+------+------+------------+------------+--------------+
| 905 | 王翰 | 男 | 2004 | 英语系 | 福建省厦门市 |
+-----+------+------+------------+------------+--------------+
1 row in set (0.00 sec)
mysql> select * from student where department in ('计算机系','英语系');
+-----+--------+------+------------+------------+--------------+
| id | name | sex | birth_year | department | address |
+-----+--------+------+------------+------------+--------------+
| 901 | 张三丰 | 男 | 2002 | 计算机系 | 北京市海淀区 |
| 904 | 李广昌 | 男 | 1999 | 英语系 | 辽宁省阜新市 |
| 905 | 王翰 | 男 | 2004 | 英语系 | 福建省厦门市 |
| 906 | 王心凌 | 女 | 1998 | 计算机系 | 湖南省衡阳市 |
+-----+--------+------+------------+------------+--------------+
4 rows in set (0.00 sec)
mysql> select * from student where birth_year>2002;
+-----+--------+------+------------+------------+--------------+
| id | name | sex | birth_year | department | address |
+-----+--------+------+------------+------------+--------------+
| 903 | 张思维 | 女 | 2003 | 中文系 | 湖南省永州市 |
| 905 | 王翰 | 男 | 2004 | 英语系 | 福建省厦门市 |
+-----+--------+------+------------+------------+--------------+
2 rows in set (0.00 sec)
mysql> select department,count(*) as total from student group by department;
+------------+-------+
| department | total |
+------------+-------+
| 计算机系 | 2 |
| 中文系 | 2 |
| 英语系 | 2 |
+------------+-------+
3 rows in set (0.01 sec)
mysql> select s2.c_name,s2.grade from student s1 join score s2 on s1.id = s2.stu_id where s1.name = '李广昌';
+--------+-------+
| c_name | grade |
+--------+-------+
| 计算机 | 70 |
| 英语 | 92 |
+--------+-------+
2 rows in set (0.01 sec)
mysql> select * from student s join score sc on s.id = sc.stu_id;
+-----+--------+------+------------+------------+--------------+----+--------+--------+-------+
| id | name | sex | birth_year | department | address | id | stu_id | c_name | grade |
+-----+--------+------+------------+------------+--------------+----+--------+--------+-------+
| 901 | 张三丰 | 男 | 2002 | 计算机系 | 北京市海淀区 | 1 | 901 | 计算机 | 98 |
| 901 | 张三丰 | 男 | 2002 | 计算机系 | 北京市海淀区 | 2 | 901 | 英语 | 80 |
| 902 | 周全有 | 男 | 2000 | 中文系 | 北京市昌平区 | 3 | 902 | 计算机 | 65 |
| 902 | 周全有 | 男 | 2000 | 中文系 | 北京市昌平区 | 4 | 902 | 中文 | 88 |
| 903 | 张思维 | 女 | 2003 | 中文系 | 湖南省永州市 | 5 | 903 | 中文 | 95 |
| 904 | 李广昌 | 男 | 1999 | 英语系 | 辽宁省阜新市 | 6 | 904 | 计算机 | 70 |
| 904 | 李广昌 | 男 | 1999 | 英语系 | 辽宁省阜新市 | 7 | 904 | 英语 | 92 |
| 905 | 王翰 | 男 | 2004 | 英语系 | 福建省厦门市 | 8 | 905 | 英语 | 94 |
| 906 | 王心凌 | 女 | 1998 | 计算机系 | 湖南省衡阳市 | 9 | 906 | 计算机 | 49 |
| 906 | 王心凌 | 女 | 1998 | 计算机系 | 湖南省衡阳市 | 10 | 906 | 英语 | 83 |
+-----+--------+------+------------+------------+--------------+----+--------+--------+-------+
10 rows in set (0.00 sec)
mysql> select stu_id,sum(grade) as total_grade from score group by stu_id;
+--------+-------------+
| stu_id | total_grade |
+--------+-------------+
| 901 | 178 |
| 902 | 153 |
| 903 | 95 |
| 904 | 162 |
| 905 | 94 |
| 906 | 132 |
+--------+-------------+
6 rows in set (0.00 sec)
mysql> select s.* from student s join score sc on s.id = sc.stu_id where sc.c_name = '计算机' and sc.grade < 95;
+-----+--------+------+------------+------------+--------------+
| id | name | sex | birth_year | department | address |
+-----+--------+------+------------+------------+--------------+
| 902 | 周全有 | 男 | 2000 | 中文系 | 北京市昌平区 |
| 904 | 李广昌 | 男 | 1999 | 英语系 | 辽宁省阜新市 |
| 906 | 王心凌 | 女 | 1998 | 计算机系 | 湖南省衡阳市 |
+-----+--------+------+------------+------------+--------------+
3 rows in set (0.00 sec)
mysql> select c_name,avg(grade) as avg_grade from score group by c_name;
+--------+-----------+
| c_name | avg_grade |
+--------+-----------+
| 计算机 | 70.5000 |
| 英语 | 87.2500 |
| 中文 | 91.5000 |
+--------+-----------+
3 rows in set (0.00 sec)
mysql> select * from score where c_name ='计算机' order by grade desc;
+----+--------+--------+-------+
| id | stu_id | c_name | grade |
+----+--------+--------+-------+
| 1 | 901 | 计算机 | 98 |
| 6 | 904 | 计算机 | 70 |
| 3 | 902 | 计算机 | 65 |
| 9 | 906 | 计算机 | 49 |
+----+--------+--------+-------+
4 rows in set (0.00 sec)
mysql> select id as student_id from student union select stu_id from score;
+------------+
| student_id |
+------------+
| 901 |
| 902 |
| 903 |
| 904 |
| 905 |
| 906 |
+------------+
6 rows in set (0.01 sec)
mysql> select s.name,s.department,sc.c_name,sc.grade from student s join score sc on s.id = sc.stu_id where s.name like
'张%' or s.name like '王%';
+--------+------------+--------+-------+
| name | department | c_name | grade |
+--------+------------+--------+-------+
| 张三丰 | 计算机系 | 计算机 | 98 |
| 张三丰 | 计算机系 | 英语 | 80 |
| 张思维 | 中文系 | 中文 | 95 |
| 王翰 | 英语系 | 英语 | 94 |
| 王心凌 | 计算机系 | 计算机 | 49 |
| 王心凌 | 计算机系 | 英语 | 83 |
+--------+------------+--------+-------+
6 rows in set (0.00 sec)