mysql之DML的select分組排序方式
一、創(chuàng)建表employee和department表
1.創(chuàng)建department表
create table department( -> depart_id int primary key auto_increment comment '部門編號', -> depart_name varchar(50) not null comment '部門名稱' -> ) auto_increment=1001;

2.創(chuàng)建employee表
create table employee( n for the right syntax to use near 'redsodsnvjnv' at line 1 -> emp_num int primary key auto_increment comment '員工編號', -> emp_name varchar(30) not null comment '員工姓名', -> emp_job varchar(30) not null comment '員工崗位', -> hire_data datetime not null comment '入職時間', -> salary int not null comment '薪資', -> bonus int not null comment '獎金', -> dept_id int comment '部門編號' -> );

3.給employee表格和department表格建立外鍵
alter table employee add constraint emp_dept_fk foreign key(dept_id) references department(depart_id);

4.給department插入數(shù)據(jù)
insert into department values(null,'科技部門'),(null,'法律部門'),(null,'后勤部門'),(null,'財務部門');

5.給employee表插入數(shù)據(jù)
insert into employee values((null,'張三','工程師','2023.9.1',12000,1000,1001),(null,'張四','工程師','2023.9.1',11000,1010,1001),(null,'李三','會計','2023.9.1',5000,300,1004),(null,'張六','保安','2023.9.1',5000,500,1003),(null,'劉律','律師','2023.9.1',1000,1,1002);

6.刪除名字為那個的數(shù)據(jù)
delete from employee where emp_name='那個';

二、分組查詢和排序查詢,以及對數(shù)據(jù)的處理(avg,sum,count,max,min)
1.根據(jù)dept_id進行分組并查詢他們的平均工資
select dept_id,avg(salary) from employee group by dept_id;

2.根據(jù)dept_id分組查詢他們年薪平均值
select dept_id, avg((salary+bonus)*12) from employee group by dept_id;

3.根據(jù)dept_id分組查詢他們薪資的最高值
select dept_id,max(salary) from employee group by dept_id;

4.根據(jù)dept_id分組查詢他們薪資的最低值
select dept_id,min(salary) from employee group by dept_id;

5.根據(jù)dept_id分組查詢他們薪資的總和
select dept_id,sum(salary) from employee group by dept_id;

6.根據(jù)dept_id分組查詢?nèi)藬?shù)的總和
select dept_id,count(*) from employee group by dept_id;

7.根據(jù)dept_id分組查詢?nèi)藬?shù)的總和
select dept_ip,count(emp_name) from employee group by dept_id;

8.按照dept_id降序的方式查詢emp_name和dept_id
select emp_name,dept_id from employee order by dept_id;

9.按照dept_id和emp_job分組查詢薪資總和
select dept_id,emp_job,sum(salary) from employee group by dept_id, emp_job;

10.在dept_id組中限制只查詢工資總和大于10000的薪資,并展現(xiàn)出來工作和薪資
select dept_id,emp_job,sum(salary) from employee group by dept_id,emp_job having sum(salary>1000);

三、select查詢之limit限制
1.查詢前三行數(shù)據(jù)
select * from employee limit 0,3;

2.查詢第三條到第七條數(shù)據(jù)
select * from employee limit 2,7;

總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Red?Hat?安裝MySQL?8.0與?Navicat的詳細過程
這篇文章主要介紹了Red?Hat安裝MySQL8.0與Navicat,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08
Mysql優(yōu)化之Zabbix分區(qū)優(yōu)化
這篇文章主要介紹了Mysql優(yōu)化中Zabbix分區(qū)優(yōu)化的詳細方法和優(yōu)缺點分析,一起學習下。2017-11-11
MySQL數(shù)據(jù)庫中ENUM的用法是什么詳解
ENUM是一個字符串對象,用于指定一組預定義的值,并可在創(chuàng)建表時使用,下面這篇文章主要介紹了MySQL數(shù)據(jù)庫中ENUM的用法是什么的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2025-06-06

