trabajo de consulta MySQL

tema de tarea

Por favor agregue una descripción de la imagen

Respuesta

Preparación

  • creación de tablas de datos
CREATE TABLE worker (
部门号 int(11) NOT NULL,
职工号 int(11) NOT NULL PRIMARY KEY,
工作时间 date NOT NULL,
工资 float(8,2) NOT NULL,
政治面貌 varchar(10) NOT NULL DEFAULT '群众',
姓名 varchar(20) NOT NULL,
出生日期 date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
  • insertar datos
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');
  • ver tabla de trabajadores
mysql> 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)

Respuesta

  • Muestra la información básica de todos los empleados.
mysql> 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)
  • Consulte los números de departamento de los departamentos a los que pertenecen todos los empleados y no muestre números de departamento duplicados.
mysql> select 部门号,count(部门号) as 人数  from worker group by 部门号;
+-----------+--------+
| 部门号    | 人数   |
+-----------+--------+
|       101 |      2 |
|       102 |      4 |
+-----------+--------+
2 rows in set (0.00 sec)
  • Encuentre el número de todos los empleados.
mysql> select count(部门号) as 总人数 from worker;
+-----------+
| 总人数    |
+-----------+
|         6 |
+-----------+
1 row in set (0.00 sec)
  • Enumere los salarios más altos y más bajos.
mysql> select max(工资) as 最高工资,min(工资) as 最低工资 from worker;
+--------------+--------------+
| 最高工资     | 最低工资     |
+--------------+--------------+
|      8500.00 |      3200.00 |
+--------------+--------------+
1 row in set (0.00 sec)
  • Enumere los salarios promedio y total de los trabajadores.
mysql> select avg(工资) as 平均工资,sum(工资) as 总工资 from worker;
+--------------+-----------+
| 平均工资     | 总工资    |
+--------------+-----------+
|  5000.000000 |  30000.00 |
+--------------+-----------+
1 row in set (0.00 sec)
  • Cree una nueva tabla con solo el número de empleado, el nombre y la asistencia al trabajo, denominada tabla de fechas de trabajo.
mysql> create table work_time select 职工号,姓名,工作时间 from worker;
Query OK, 6 rows affected (0.01 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> 
mysql> 
mysql> select * from work_time;
+-----------+--------+--------------+
| 职工号    | 姓名   | 工作时间     |
+-----------+--------+--------------+
|      1001 | 张三   | 2015-05-04   |
|      1002 | 李四   | 2017-02-06   |
|      1003 | 王亮   | 2011-01-04   |
|      1004 | 赵六   | 2016-10-10   |
|      1005 | 钱七   | 2014-04-01   |
|      1006 | 孙八   | 2017-05-05   |
+-----------+--------+--------------+
6 rows in set (0.00 sec)
  • Enumere el número de empleado, el nombre y la fecha de nacimiento de todos los empleados de apellido Liu.
mysql> select 职工号,姓名,出生日期 from worker where 姓名 like '刘%';
Empty set (0.00 sec)
  • Enumere los nombres y fechas de empleo de los empleados nacidos antes de 1960.
mysql> select 姓名,工作时间,出生日期 from worker where 出生日期 < '1960-01-01';
Empty set (0.00 sec)
  • Enumere los nombres de todos los empleados cuyo salario está entre 1000 y 2000.
mysql> select 姓名,工资 from worker where 工资 between 1000 and 2000;
Empty set (0.00 sec)
  • Enumere los nombres de todos los empleados con los apellidos Chen y Li.
mysql> select 姓名 from worker where 姓名 like '陈%' or 姓名 like '李%';
+--------+
| 姓名   |
+--------+
| 李四   |
+--------+
1 row in set (0.00 sec)
  • Enumere todos los números de empleados, nombres y miembros del grupo cuyos números de departamento sean 2 y 3.
mysql> select 职工号,姓名,政治面貌 from worker where 政治面貌 = '党员' and 职工号 like '%2' or 职工号 like '%3';
+-----------+--------+--------------+
| 职工号    | 姓名   | 政治面貌     |
+-----------+--------+--------------+
|      1003 | 王亮   | 党员         |
+-----------+--------+--------------+
1 row in set (0.00 sec)
  • Ordene a los empleados en la tabla de empleados por orden de nacimiento.
mysql> select * from worker order by 出生日期;
+-----------+-----------+--------------+---------+--------------+--------+--------------+
| 部门号    | 职工号    | 工作时间     | 工资    | 政治面貌     | 姓名   | 出生日期     |
+-----------+-----------+--------------+---------+--------------+--------+--------------+
|       102 |      1003 | 2011-01-04   | 8500.00 | 党员         | 王亮   | 1983-06-08   |
|       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 |      1002 | 2017-02-06   | 3200.00 | 团员         | 李四   | 1997-02-08   |
+-----------+-----------+--------------+---------+--------------+--------+--------------+
6 rows in set (0.00 sec)
  • Muestre los números de empleados y los nombres de los 3 mejores empleados con el salario más alto.
mysql> select 职工号,姓名,工资 from worker order by 工资 desc limit 3;
+-----------+--------+---------+
| 职工号    | 姓名   | 工资    |
+-----------+--------+---------+
|      1003 | 王亮   | 8500.00 |
|      1004 | 赵六   | 5500.00 |
|      1005 | 钱七   | 4800.00 |
+-----------+--------+---------+
3 rows in set (0.00 sec)
  • Encuentre el número de miembros del partido en cada departamento.
mysql> select count(政治面貌) as 党员总人数 from worker where 政治面貌 ='党员';
+-----------------+
| 党员总人数      |
+-----------------+
|               3 |
+-----------------+
1 row in set (0.00 sec)
  • Estadísticas de salario y salario promedio de cada departamento
mysql> select 部门号,avg(工资) as 平均工资 from worker group by 部门号;
+-----------+--------------+
| 部门号    | 平均工资     |
+-----------+--------------+
|       101 |  3350.000000 |
|       102 |  5825.000000 |
+-----------+--------------+
2 rows in set (0.00 sec)
  • Enumere los números de departamento y el número total de personas cuyo número total es mayor que 4.
mysql> select 部门号,count(部门号)  总人数 from worker group by 部门号 having 总人数 >= 4;
+-----------+-----------+
| 部门号    | 总人数    |
+-----------+-----------+
|       102 |         4 |
+-----------+-----------+
1 row in set (0.00 sec)

Supongo que te gusta

Origin blog.csdn.net/bo1029/article/details/131625370
Recomendado
Clasificación