Database job - select query operation

database job

insert image description here
insert image description here

create database

mysql> create table worker(                                                -> 部门号 int(11) not null,                                            -> 职工号 int(11) primary key not null,
    -> 工作时间 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;  

insert image description here
insert data

mysql> insert into worker(部门号,职工号,工作时间,工资,政治面貌,姓名,出生日期) values(101,1001,'2015-5-4',3500.00,'群众','张三','1990-7-1');

mysql> insert into worker(部门号,职工号,工作时间,工资,政治面貌,姓名,出生日期) values(101,1002,'2017-2-6',3200.00,'团员','李四','1997-2-8');

mysql> insert into worker(部门号,职工号,工作时间,工资,政治面貌,姓名,出生日期) values(102,1003,'2011-1-4',8500.00,'党员','王亮','1983-6-8');

mysql> insert into worker(部门号,职工号,工作时间,工资,政治面貌,姓名,出生日期) values(102,1004,'2016-10-10',5500.00,'群众','赵六','1994-9-5');

mysql> insert into worker(部门号,职工号,工作时间,工资,政治面貌,姓名,出生日期) values(102,1005,'2014-4-1',4800.00,'党员','钱七','1992-12-30');

mysql> insert into worker(部门号,职工号,工作时间,工资,政治面貌,姓名,出生日期) values(102,1006,'2017-5-5',4500.00,'党员','孙八','1996-9-2');

insert image description here

1. Display the basic information of all employees.

mysql> select * from worker;

insert image description here

2. Query the department numbers of the departments to which all employees belong, and do not display duplicate department numbers.

mysql> select distinct 部门号 from worker;

insert image description here

3. Find the number of all employees.

mysql> select count(职工号) as 总人数 from worker;

insert image description here
4. List the highest and lowest wages.

mysql> select max(工资) as 最高工资, min(工资) as 最低工资 from worker;

insert image description here
5. List the average salary and total salary of employees.

mysql> select avg(工资) as 平均工资, sum(工资) as 总工资 from worker;

insert image description here

6. Create a new table with only the employee number, name and work participation, called the work date table.

mysql> create table work_date select 职工号,姓名,工作时间 from worker;

insert image description here
7. Display the ages of all female employees.
Insert gender into the table structure and create constraints

mysql> alter table worker add 性别 varchar(5);
mysql> alter table worker add constraint ck_worker_gender check(性别 in ('男','女'));

modify gender

mysql> update worker set 性别='女' where 职工号=1001;

mysql> update worker set 性别='男' where 职工号=1002;

mysql> update worker set 性别='男' where 职工号=1003;

mysql> update worker set 性别='男' where 职工号=1004;

mysql> update worker set 性别='女' where 职工号=1005;

mysql> update worker set 性别='女' where 职工号=1006;

insert image description here

Mysql has built-in functions that can be used, among which the curdate() function is to obtain the current date of the computer, and the timestampdiff() function can compare the current date with the date of birth to calculate the age
insert image description here

mysql> mysql> select 职工号,姓名,性别,timestampdiff(year, 出生日期, curdate()) as 年龄 from worker where 性别='女';

insert image description here

8. List the employee number, name and date of birth of all employees surnamed Liu.

mysql> select 职工号,姓名,出生日期 from worker where 姓名 like '刘%';

insert image description here

9. List the names and working dates of employees born before 1960.

mysql> select 姓名,工作时间  from worker where 出生日期 < '1960-1-1';

insert image description here

10. List the names of all employees whose salary is between 1000-2000.

mysql> select 姓名 from worker where 工资 < 2000 and 工资 > 1000;

insert image description here

11. List the names of all employees surnamed Chen and Li.

mysql> select 姓名 from worker where 姓名 like '陈%' or 姓名 like '李%';

insert image description here

12. List all employee numbers, names, and party members whose department numbers are 2 and 3.

mysql> select 职工号,姓名,政治面貌 from worker where 部门号=102 or 部门号=103;

insert image description here

13. Sort the employees in the employee table worker according to the order of birth.

mysql> select * from worker order by 出生日期;

insert image description here

14. Display the employee numbers and names of the top 3 employees with the highest salary.

mysql> select 职工号,姓名 from worker order by 工资 desc limit 3;

insert image description here

15. Find the number of party members in each department.

mysql> select 部门号, count(*) as 党员人数 from worker where 政治面貌='党员' group by 部门号;

insert image description here

16. Statistics of wages and average wages of various departments

mysql> select 部门号, sum(工资) as 总工资, avg(工资) as 平均工资 from worker  group by 部门号;

insert image description here

17. List the department numbers and the total number of people whose total number is greater than 4.

mysql> select 部门号, count(*) as 总人数 from worker group by 部门号 having 总人数 > 4;

insert image description here

Guess you like

Origin blog.csdn.net/weixin_55822200/article/details/131625072