mysql,对表的简单操作

news/2024/10/30 21:32:27/

一.创建表并插入数据 

mysql> create table worker(-> department_id int(11) not null comment '部门号',-> worker_id int(11) primary key not null comment '职工号',-> worker_date date not null comment '工作时间',-> wages float(8,2) not null comment '工资',-> politics varchar(10) not null default '群众' comment '政治面貌',-> name varchar(20) not null comment '姓名',-> borth_date date not null comment '出生日期'-> )engine=InnoDB default charset=utf8 row_format=dynamic;
Query OK, 0 rows affected, 4 warnings (0.02 sec)
mysql> insert into worker (department_id,worker_id,worker_date,wages,politics,name,borth_date) values (101,1001,'2015-5-4',3500.00,'群众','张三','1990-7-1');
Query OK, 1 row affected (0.01 sec)mysql> insert into worker (department_id,worker_id,worker_date,wages,politics,name,borth_date) values (101,1001,'2017-2-6',3200.00,'团员','李四','1997-2-8');
Query OK, 1 row affected (0.00 sec)mysql> insert into worker (department_id,worker_id,worker_date,wages,politics,name,borth_date) values (102,1003,'2011-1-4',8500.00,'党员','王亮','1983-6-8');
Query OK, 1 row affected (0.00 sec)mysql> insert into worker (department_id,worker_id,worker_date,wages,politics,name,borth_date) values (102,1004,'2016-10-10',5500.00,'群众','赵六','1994-9-5');
Query OK, 1 row affected (0.00 sec)mysql> insert into worker (department_id,worker_id,worker_date,wages,politics,name,borth_date) values (102,1005,'2014-4-1',4800.00,'党员','钱七','1992-12-30');
Query OK, 1 row affected (0.01 sec)mysql> insert into worker (department_id,worker_id,worker_date,wages,politics,name,borth_date) values (102,1006,'2017-5-5',4500.00,'党员','孙八','1996-9-2');
Query OK, 1 row affected (0.00 sec

1、显示所有职工的基本信息

select *from worker;
+---------------+-----------+-------------+---------+----------+--------+------------+
| department_id | worker_id | worker_date | wages   | politics | name   | borth_date |
+---------------+-----------+-------------+---------+----------+--------+------------+
|           101 |      1001 | 2015-05-04  | 3500.00 | 群众     | 张三   | 1990-07-01 |
|           101 |      1001 | 2015-05-04  | 3500.00 | 群众     | 张三   | 1990-07-01 |
|           101 |      1001 | 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 |
+---------------+-----------+-------------+---------+----------+--------+------------+
7 rows in set (0.00 sec)

2、查询所有职工所属部门的部门号,不显示重复的部门号

mysql> select distinct department_id from worker;
+---------------+
| department_id |
+---------------+
|           101 |
|           102 |
+---------------+
2 rows in set (0.02 sec)

3、求出所有职工的人数。

mysql> select count(name) from worker;
+-------------+
| count(name) |
+-------------+
|           7 |
+-------------+
1 row in set (0.04 sec)


4、列出最高工和最低工资。

mysql> select max(wages) as '最高工资',min(wages) as '最低工资' from worker;
+--------------+--------------+
| 最高工资     | 最低工资     |
+--------------+--------------+
|      8500.00 |      3200.00 |
+--------------+--------------+
1 row in set (0.01 sec)


5、列出职工的平均工资和总工资。

mysql> select avg(wages) as '平均工资',sum(wages) as '总工资' from worker;
+--------------+-----------+
| 平均工资     | 总工资    |
+--------------+-----------+
|  4785.714286 |  33500.00 |
+--------------+-----------+
1 row in set (0.01 sec)


6、创建一个只有职工号、姓名和参加工作的新表,名为工作日期表。

mysql> create table workdate select worker_id,name from worker;
Query OK, 7 rows affected (0.02 sec)
Records: 7  Duplicates: 0  Warnings: 0


7、列出1960年以前出生的职工的姓名、参加工作日期。

mysql> select name,worker_date from worker where borth_date < '1960-1-1';
Empty set (0.02 sec)

8、列出工资在1000-2000之间的所有职工姓名。

mysql> select name from worker where wages > 1000 and wages < 2000;
Empty set (0.00 sec)

9、列出所有陈姓和李姓的职工姓名。

mysql> select name from worker where name like '陈%' or name like '李%';
+--------+
| name   |
+--------+
| 李四   |
+--------+
1 row in set (0.01 sec)


10、列出所有部门号为2和3的职工号、姓名、党员否。

mysql> select worker_id,name,politics from worker where politics!='党员' and department_id<=>102 or department_id<=>103;
+-----------+--------+----------+
| worker_id | name   | politics |
+-----------+--------+----------+
|      1004 | 赵六   | 群众     |
+-----------+--------+----------+
1 row in set (0.00 sec)

11、将职工表worker中的职工按出生的先后顺序排序。

mysql> select *from worker order by borth_date;
+---------------+-----------+-------------+---------+----------+--------+------------+
| department_id | worker_id | worker_date | wages   | politics | name   | borth_date |
+---------------+-----------+-------------+---------+----------+--------+------------+
|           102 |      1003 | 2011-01-04  | 8500.00 | 党员     | 王亮   | 1983-06-08 |
|           101 |      1001 | 2015-05-04  | 3500.00 | 群众     | 张三   | 1990-07-01 |
|           101 |      1001 | 2015-05-04  | 3500.00 | 群众     | 张三   | 1990-07-01 |
|           102 |      1005 | 2014-04-01  | 4800.00 | 党员     | 钱七   | 1992-12-30 |
|           102 |      1004 | 2016-10-10  | 5500.00 | 群众     | 赵六   | 1994-09-05 |
|           102 |      1006 | 2017-05-05  | 4500.00 | 党员     | 孙八   | 1996-09-02 |
|           101 |      1001 | 2017-02-06  | 3200.00 | 团员     | 李四   | 1997-02-08 |
+---------------+-----------+-------------+---------+----------+--------+------------+
7 rows in set (0.00 sec)

12、显示工资最高的前3名职工的职工号和姓名。

mysql> select worker_id,name from worker order by wages desc limit 0,3;
+-----------+--------+
| worker_id | name   |
+-----------+--------+
|      1003 | 王亮   |
|      1004 | 赵六   |
|      1005 | 钱七   |
+-----------+--------+
3 rows in set (0.00 sec)

13、求出各部门党员的人数。

mysql> select department_id,count(name) from worker where politics='党员'group by department_id; 
+---------------+-------------+
| department_id | count(name) |
+---------------+-------------+
|           102 |           3 |
+---------------+-------------+
1 row in set (0.00 sec)


14、统计各部门的工资和平均工资

mysql> select department_id,sum(wages),avg(wages) from worker group by department_id;
+---------------+------------+-------------+
| department_id | sum(wages) | avg(wages)  |
+---------------+------------+-------------+
|           101 |   10200.00 | 3400.000000 |
|           102 |   23300.00 | 5825.000000 |
+---------------+------------+-------------+
2 rows in set (0.00 sec


15、列出总人数大于4的部门号和总人数

select department_id,count(department_id) from worker group by(department_id) having count(department_id) > 4;
Empty set (0.00 sec)


http://www.ppmy.cn/news/836498.html

相关文章

SpringBoot快速实践 --Ⅰ

文章目录 启动一个SpringBoot项目如何替换内嵌容器玩转SpringBoot配置全局异常处理过滤器拦截器使用Lombok简洁代码使用IDEA HTTP Client进行接口调试 启动一个SpringBoot项目 如果你觉得使用官网来创建太慢了&#xff0c;那你直接把以前项目的依赖粘过来就行了&#xff1a; …

【sfu】追踪ms如何提供flv服务

media request : mrmedia source : msMediaSourceMgr 根据mr 创建ms std::shared_ptr<MediaSource> MediaSourceMgr::FetchOrCreateSource(MediaSource::Config& cfg,std::shared_ptr<MediaRequest> req)

Freepiano如何使用sf2音源,sfark如何转换至sf2?

补充2&#xff1a; 我发现 Riccardo Loi has created two other piano files in SFZ format while running his Keppys Studio. Here they are:Keppys Steinway Piano (as a 7z compressed file, compressed size 232MB).Keppys Farfisa Grand Piano (as a 7z compressed file…

ISP-ASF

1. 概述 1.1 高频与低频区分&#xff1a; 如何区分图像的高频信息和低频信息&#xff0c;所谓高频就是该像素点与周围像素差异较大&#xff0c;常见于一副图像的边缘细节和噪声等&#xff1b;而低频就是该像素点与周围像素差异变化不大&#xff0c;一般体现为图像的平坦区&am…

elasitcsearch管道应用 加工字段

需求 根据 字段 1 和字段2来加工字段3 当 字段 1 2 没有值 的时候不加工 并且字段四 是固定只 使用管道来 解决 1 创建管道 PUT _ingest/pipeline/activity_allintenet_pipeline {"description": " Network wide travel data ","processors": […

k8s(Kubernetes)设置 pod,Deployment 域名自定义映射ip,hosts 解析 HostAliases

核心 hostAliases:# 解决服务器内,因路由器配置不全&#xff0c; 不能访问外网问题- hostnames:- graph.requarks.ioip: 104.26.14.122案例 wiki k8s Deployment 直接 编辑修改 Deployment kubectl edit Deployment wiki最后内容如下&#xff0c;如需保存 按 esc 键&#xff…

Radeon 驱动升级到20.x后如何设置显卡切换

笔记本是E480&#xff0c;intel集成显卡AMD独显双显卡。 以前Radeon 19.x版本显卡驱动设置里有个页面可以指定程序使用的显卡&#xff0c;但是升级到Radeno 20.x后&#xff0c;显卡驱动中就没有这个设置了。 在windows10 1909中这个设置在下面这个地方&#xff1a; 设置&…

c# opencv 将Mat追加到已有的npy文件

//读取已有的npy文件NDArray masks np.load("I:\\软件\\1.npy"); List<NDArray> ndList new List<NDArray>();if (masksnull){}else{//读取npy文件已有的数据for (int i 0; i < masks.shape[2]; i){var roi masks[$":,:,{i}"];ndLis…