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;
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
MySQL數(shù)據(jù)遷移至達夢數(shù)據(jù)庫的詳細教程
這篇文章主要為大家詳細介紹了MySQL數(shù)據(jù)遷移至達夢數(shù)據(jù)庫的詳細教程,文中通過示例圖片進行了詳細的介紹,有需要的小伙伴可以參考一下2025-03-03MYSQL ERROR 1045 (28000): Access denied for user (using pass
Mysql中添加用戶之后可能出現(xiàn)登錄時提示ERROR 1045 (28000): Access denied for user的錯誤.2009-07-07