创建表并添加数据
mysql8.0 [db_test]>create table worker(
-> 部门号 int(11) not null,
-> 职工号 int(11) not null,
-> 工作时间 date not null,
-> 工资 float(8,2) not null,
-> 政治面貌 varchar(10) not null default '群众',
-> 姓名 varchar(20) not null,
-> 出生日期 date not null,
-> primary key'职工号')
-> ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
Query OK, 0 rows affected (0.01 sec)
mysql8.0 [db_test]>INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生日期`)
-> VALUES
-> (101, 1001, '2015-5-4', 3500.00, '群众', '张三', '1990-7-1'),
-> (101, 1002, '2017-2-6', 3200.00, '团员', '李四', '1997-2-8'),
-> (102, 1003, '2011-1-4', 8500.00, '党员', '王亮', '1983-6-8'),
-> (102, 1004, '2016-10-10', 5500.00, '群众', '赵六', '1994-9-5'),
-> (102, 1005, '2014-4-1', 4800.00, '党员', '钱七', '1992-12-30'),
-> (102, 1006, '2017-5-5', 4500.00, '党员', '孙八', '1996-9-2');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
查看表结构和表信息
mysql8.0 [db_test]>desc worker;
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| 部门号 | int(11) | NO | | NULL | |
| 职工号 | int(11) | NO | PRI | NULL | |
| 工作时间 | date | NO | | NULL | |
| 工资 | float(8,2) | NO | | NULL | |
| 政治面貌 | varchar(10) | NO | | 群众 | |
| 姓名 | varchar(20) | NO | | NULL | |
| 出生日期 | date | NO | | NULL | |
+--------------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
mysql8.0 [db_test]>select * from worker;
+-----------+-----------+--------------+---------+--------------+--------+--------------+
| 部门号 | 职工号 | 工作时间 | 工资 | 政治面貌 | 姓名 | 出生日期 |
+-----------+-----------+--------------+---------+--------------+--------+--------------+
| 101 | 1001 | 2015-05-04 | 3500.00 | 群众 | 张三 | 1990-07-01 |
| 101 | 1002 | 2017-02-06 | 3200.00 | 团员 | 李四 | 1997-02-08 |
| 102 | 1003 | 2011-01-04 | 8500.00 | 党员 | 王亮 | 1983-06-08 |
| 102 | 1004 | 2016-10-10 | 5500.00 | 群众 | 赵六 | 1994-09-05 |
| 102 | 1005 | 2014-04-01 | 4800.00 | 党员 | 钱七 | 1992-12-30 |
| 102 | 1006 | 2017-05-05 | 4500.00 | 党员 | 孙八 | 1996-09-02 |
+-----------+-----------+--------------+---------+--------------+--------+--------------+
6 rows in set (0.00 sec)
mysql8.0 [db_test]>select * from worker;
+-----------+-----------+--------------+---------+--------------+--------+--------------+
| 部门号 | 职工号 | 工作时间 | 工资 | 政治面貌 | 姓名 | 出生日期 |
+-----------+-----------+--------------+---------+--------------+--------+--------------+
| 101 | 1001 | 2015-05-04 | 3500.00 | 群众 | 张三 | 1990-07-01 |
| 101 | 1002 | 2017-02-06 | 3200.00 | 团员 | 李四 | 1997-02-08 |
| 102 | 1003 | 2011-01-04 | 8500.00 | 党员 | 王亮 | 1983-06-08 |
| 102 | 1004 | 2016-10-10 | 5500.00 | 群众 | 赵六 | 1994-09-05 |
| 102 | 1005 | 2014-04-01 | 4800.00 | 党员 | 钱七 | 1992-12-30 |
| 102 | 1006 | 2017-05-05 | 4500.00 | 党员 | 孙八 | 1996-09-02 |
+-----------+-----------+--------------+---------+--------------+--------+--------------+
6 rows in set (0.00 sec)
mysql8.0 [db_test]>select distinct 部门号 from worker;
+-----------+
| 部门号 |
+-----------+
| 101 |
| 102 |
+-----------+
2 rows in set (0.00 sec)
mysql8.0 [db_test]>select count(*) from worker;
+----------+
| count(*) |
+----------+
| 6 |
+----------+
1 row in set (0.00 sec)
mysql8.0 [db_test]>select max(工资),min(工资) from worker;
+-------------+-------------+
| max(工资) | min(工资) |
+-------------+-------------+
| 8500.00 | 3200.00 |
+-------------+-------------+
1 row in set (0.00 sec)
mysql8.0 [db_test]>select sum(工资),avg(工资) from worker;
+-------------+-------------+
| sum(工资) | avg(工资) |
+-------------+-------------+
| 30000.00 | 5000.000000 |
+-------------+-------------+
1 row in set (0.00 sec)
mysql8.0 [db_test]>create table worktime select 职工号,姓名,工作时间 from workeer;
Query OK, 6 rows affected (0.01 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql8.0 [db_test]>desc worktime;
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| 职工号 | int(11) | NO | | NULL | |
| 姓名 | varchar(20) | NO | | NULL | |
| 工作时间 | date | NO | | NULL | |
+--------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql8.0 [db_test]>select 2024 - year(出生日期) as 年龄 from worker;
+--------+
| 年龄 |
+--------+
| 34 |
| 27 |
| 41 |
| 30 |
| 32 |
| 28 |
+--------+
6 rows in set (0.00 sec)
mysql8.0 [db_test]>select 职工号,姓名,出生日期
-> from worker
-> where 姓名 like '刘%';
Empty set (0.00 sec)
mysql8.0 [db_test]>select 姓名,工作时间
-> from worker
-> where 出生日期 < '1960-01-01';
Empty set (0.00 sec)
mysql8.0 [db_test]>select 姓名
-> from worker
-> where 工资 between 1000 and 2000;
Empty set (0.00 sec)
mysql8.0 [db_test]>select 姓名
-> from worker
-> where 姓名 like '张%' or 姓名 like '李%';
+--------+
| 姓名 |
+--------+
| 张三 |
| 李四 |
+--------+
2 rows in set (0.00 sec)
mysql8.0 [db_test]>select 职工号,姓名,政治面貌 from worker where 部门号= '3' orr '2';
+-----------+--------+--------------+
| 职工号 | 姓名 | 政治面貌 |
+-----------+--------+--------------+
| 1001 | 张三 | 群众 |
| 1002 | 李四 | 团员 |
| 1003 | 王亮 | 党员 |
| 1004 | 赵六 | 群众 |
| 1005 | 钱七 | 党员 |
| 1006 | 孙八 | 党员 |
+-----------+--------+--------------+
6 rows in set (0.00 sec)
mysql8.0 [db_test]>select 姓名 from worker order by 出生日期;
+--------+
| 姓名 |
+--------+
| 王亮 |
| 张三 |
| 钱七 |
| 赵六 |
| 孙八 |
| 李四 |
+--------+
6 rows in set (0.00 sec)
mysql8.0 [db_test]>select 职工号,姓名,工资
-> from worker
-> order by 工资 desc limit 3;
+-----------+--------+---------+
| 职工号 | 姓名 | 工资 |
+-----------+--------+---------+
| 1003 | 王亮 | 8500.00 |
| 1004 | 赵六 | 5500.00 |
| 1005 | 钱七 | 4800.00 |
+-----------+--------+---------+
3 rows in set (0.00 sec)
mysql8.0 [db_test]>select 部门号,count(*)
-> from worker
-> where 政治面貌='党员' group by 部门号;
+-----------+----------+
| 部门号 | count(*) |
+-----------+----------+
| 102 | 3 |
+-----------+----------+
1 row in set (0.00 sec)
mysql8.0 [db_test]>
mysql8.0 [db_test]>select 部门号,sum(工资),avg(工资)
-> from worker
-> group by 部门号;
+-----------+-------------+-------------+
| 部门号 | sum(工资) | avg(工资) |
+-----------+-------------+-------------+
| 101 | 6700.00 | 3350.000000 |
| 102 | 23300.00 | 5825.000000 |
+-----------+-------------+-------------+
2 rows in set (0.00 sec)
mysql8.0 [db_test]>select 部门号,count(*)
-> from worker
-> group by 部门号 having count(*) > 4;
Empty set (0.00 sec)
mysql8.0 [db_test]>create table student(
-> id int(10) not null unique primary key,
-> name varchar(20) not null,
-> sex varchar(4),
-> brith year,
-> department varchar(20),
-> address varchar(50)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql8.0 [db_test]>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)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql8.0 [db_test]>insert into student(id,name,sex,brith,department,address)
-> values
-> ( 901,'张老大', '男',1985,'计算机系', '北京市海淀区'),
-> ( 902,'张老二', '男',1986,'中文系', '北京市昌平区'),
-> ( 903,'张三', '女',1990,'中文系', '湖南省永州市'),
-> ( 904,'李四', '男',1990,'英语系', '辽宁省阜新市'),
-> ( 905,'王五', '女',1991,'英语系', '福建省厦门市'),
-> ( 906,'王六', '男',1988,'计算机系', '湖南省衡阳市');
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql8.0 [db_test]>insert into score(id,stu_id,c_name,grade)
-> 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, '计算机',90),
-> (NULL,906, '英语',85);
Query OK, 10 rows affected (0.00 sec)
Records: 10 Duplicates: 0 Warnings: 0
mysql8.0 [db_test]>select * from student;
+-----+-----------+------+-------+--------------+--------------------+
| id | name | sex | brith | department | address |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 |
| 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 |
| 903 | 张三 | 女 | 1990 | 中文系 | 湖南省永州市 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 905 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
+-----+-----------+------+-------+--------------+--------------------+
6 rows in set (0.00 sec)
mysql8.0 [db_test]>select *
-> from student
-> limit 1,3;
+-----+-----------+------+-------+------------+--------------------+
| id | name | sex | brith | department | address |
+-----+-----------+------+-------+------------+--------------------+
| 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 |
| 903 | 张三 | 女 | 1990 | 中文系 | 湖南省永州市 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
+-----+-----------+------+-------+------------+--------------------+
3 rows in set (0.00 sec)
mysql8.0 [db_test]>select id,name,department
-> from student;
+-----+-----------+--------------+
| id | name | department |
+-----+-----------+--------------+
| 901 | 张老大 | 计算机系 |
| 902 | 张老二 | 中文系 |
| 903 | 张三 | 中文系 |
| 904 | 李四 | 英语系 |
| 905 | 王五 | 英语系 |
| 906 | 王六 | 计算机系 |
+-----+-----------+--------------+
6 rows in set (0.00 sec)
mysql8.0 [db_test]>select *
-> from student
-> where department = '计算机系' or department = '英语系';
+-----+-----------+------+-------+--------------+--------------------+
| id | name | sex | brith | department | address |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 905 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
+-----+-----------+------+-------+--------------+--------------------+
4 rows in set (0.00 sec)
mysql8.0 [db_test]>select * from student where (2024-brith) between 28 and 32;
Empty set (0.00 sec)
mysql8.0 [db_test]>select department,count(*) 人数
-> from student
-> group by department;
+--------------+--------+
| department | 人数 |
+--------------+--------+
| 中文系 | 2 |
| 英语系 | 2 |
| 计算机系 | 2 |
+--------------+--------+
3 rows in set (0.00 sec)
mysql8.0 [db_test]>select c_name,max(grade)
-> from score
-> group by c_name;
+-----------+------------+
| c_name | max(grade) |
+-----------+------------+
| 中文 | 95 |
| 英语 | 94 |
| 计算机 | 98 |
+-----------+------------+
3 rows in set (0.00 sec)
mysql8.0 [db_test]>select c.c_name,c.grade
-> from student
-> d,score c where d.id = c.stu_id and d.name = '李四';
+-----------+-------+
| c_name | grade |
+-----------+-------+
| 计算机 | 70 |
| 英语 | 92 |
+-----------+-------+
2 rows in set (0.00 sec)
mysql8.0 [db_test]>select d.id,name,sex,brith,department,address,c_name,grade
-> from student
-> d,score c where d.id = c.stu_id;
+-----+-----------+------+-------+--------------+--------------------+-----------+-------+
| id | name | sex | brith | department | address | c_name | grade |
+-----+-----------+------+-------+--------------+--------------------+-----------+-------+
| 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 | 计算机 | 98 |
| 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 | 英语 | 80 |
| 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 | 计算机 | 65 |
| 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 | 中文 | 88 |
| 903 | 张三 | 女 | 1990 | 中文系 | 湖南省永州市 | 中文 | 95 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 | 计算机 | 70 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 | 英语 | 92 |
| 905 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 | 英语 | 94 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 | 计算机 | 90 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 | 英语 | 85 |
+-----+-----------+------+-------+--------------+--------------------+-----------+-------+
10 rows in set (0.00 sec)
mysql8.0 [db_test]>select d.id,name,sum(grade)
-> from student
-> d,score c where d.id = c.stu_id group by id;
+-----+-----------+------------+
| id | name | sum(grade) |
+-----+-----------+------------+
| 901 | 张老大 | 178 |
| 902 | 张老二 | 153 |
| 903 | 张三 | 95 |
| 904 | 李四 | 162 |
| 905 | 王五 | 94 |
| 906 | 王六 | 175 |
+-----+-----------+------------+
6 rows in set (0.00 sec)
mysql8.0 [db_test]>select c_name,avg(grade)
-> from score
-> group by c_name;
+-----------+------------+
| c_name | avg(grade) |
+-----------+------------+
| 中文 | 91.5000 |
| 英语 | 87.7500 |
| 计算机 | 80.7500 |
+-----------+------------+
3 rows in set (0.00 sec)
mysql8.0 [db_test]>select * from student where id = any(select stu_id from score where c_name = '计算机' and grade < 95);
+-----+-----------+------+-------+--------------+--------------------+
| id | name | sex | brith | department | address |
+-----+-----------+------+-------+--------------+--------------------+
| 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
+-----+-----------+------+-------+--------------+--------------------+
3 rows in set (0.00 sec)
mysql8.0 [db_test]>select *
-> from student
-> where id = any(
-> select stu_id
-> from score
-> where c_name = '计算机' and c_name='英语');
Empty set (0.00 sec)
mysql8.0 [db_test]>select stu_id,grade
-> from score
-> where c_name = '计算机' order by grade desc;
+--------+-------+
| stu_id | grade |
+--------+-------+
| 901 | 98 |
| 906 | 90 |
| 904 | 70 |
| 902 | 65 |
+--------+-------+
4 rows in set (0.00 sec)
mysql8.0 [db_test]>select id
-> from student
-> union select stu_id
-> from score;
+-----+
| id |
+-----+
| 901 |
| 902 |
| 903 |
| 904 |
| 905 |
| 906 |
+-----+
6 rows in set (0.00 sec)
mysql8.0 [db_test]>select name,department,c_name,grade
-> from student,score
-> where student.id=score.stu_id and (name like '张%' or name like '王%');
+-----------+--------------+-----------+-------+
| name | department | c_name | grade |
+-----------+--------------+-----------+-------+
| 张老大 | 计算机系 | 计算机 | 98 |
| 张老大 | 计算机系 | 英语 | 80 |
| 张老二 | 中文系 | 计算机 | 65 |
| 张老二 | 中文系 | 中文 | 88 |
| 张三 | 中文系 | 中文 | 95 |
| 王五 | 英语系 | 英语 | 94 |
| 王六 | 计算机系 | 计算机 | 90 |
| 王六 | 计算机系 | 英语 | 85 |
+-----------+--------------+-----------+-------+
8 rows in set (0.00 sec)
mysql8.0 [db_test]>select name,brith,department,c_name,grade
-> from student,score
-> where student.id = score.stu_id and address like '湖南%';
+--------+-------+--------------+-----------+-------+
| name | brith | department | c_name | grade |
+--------+-------+--------------+-----------+-------+
| 张三 | 1990 | 中文系 | 中文 | 95 |
| 王六 | 1988 | 计算机系 | 计算机 | 90 |
| 王六 | 1988 | 计算机系 | 英语 | 85 |
+--------+-------+--------------+-----------+-------+
3 rows in set (0.00 sec)