mysql之DML的select分組排序方式
一、創(chuàng)建表employee和department表
1.創(chuàng)建department表
create table department( -> depart_id int primary key auto_increment comment '部門(mén)編號(hào)', -> depart_name varchar(50) not null comment '部門(mén)名稱(chēng)' -> ) 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 '員工編號(hào)', -> emp_name varchar(30) not null comment '員工姓名', -> emp_job varchar(30) not null comment '員工崗位', -> hire_data datetime not null comment '入職時(shí)間', -> salary int not null comment '薪資', -> bonus int not null comment '獎(jiǎng)金', -> dept_id int comment '部門(mén)編號(hào)' -> );
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,'科技部門(mén)'),(null,'法律部門(mén)'),(null,'后勤部門(mén)'),(null,'財(cái)務(wù)部門(mén)');
5.給employee表插入數(shù)據(jù)
insert into employee values((null,'張三','工程師','2023.9.1',12000,1000,1001),(null,'張四','工程師','2023.9.1',11000,1010,1001),(null,'李三','會(huì)計(jì)','2023.9.1',5000,300,1004),(null,'張六','保安','2023.9.1',5000,500,1003),(null,'劉律','律師','2023.9.1',1000,1,1002);
6.刪除名字為那個(gè)的數(shù)據(jù)
delete from employee where emp_name='那個(gè)';
二、分組查詢(xún)和排序查詢(xún),以及對(duì)數(shù)據(jù)的處理(avg,sum,count,max,min)
1.根據(jù)dept_id進(jìn)行分組并查詢(xún)他們的平均工資
select dept_id,avg(salary) from employee group by dept_id;
2.根據(jù)dept_id分組查詢(xún)他們年薪平均值
select dept_id, avg((salary+bonus)*12) from employee group by dept_id;
3.根據(jù)dept_id分組查詢(xún)他們薪資的最高值
select dept_id,max(salary) from employee group by dept_id;
4.根據(jù)dept_id分組查詢(xún)他們薪資的最低值
select dept_id,min(salary) from employee group by dept_id;
5.根據(jù)dept_id分組查詢(xún)他們薪資的總和
select dept_id,sum(salary) from employee group by dept_id;
6.根據(jù)dept_id分組查詢(xún)?nèi)藬?shù)的總和
select dept_id,count(*) from employee group by dept_id;
7.根據(jù)dept_id分組查詢(xún)?nèi)藬?shù)的總和
select dept_ip,count(emp_name) from employee group by dept_id;
8.按照dept_id降序的方式查詢(xún)emp_name和dept_id
select emp_name,dept_id from employee order by dept_id;
9.按照dept_id和emp_job分組查詢(xún)薪資總和
select dept_id,emp_job,sum(salary) from employee group by dept_id, emp_job;
10.在dept_id組中限制只查詢(xún)工資總和大于10000的薪資,并展現(xiàn)出來(lái)工作和薪資
select dept_id,emp_job,sum(salary) from employee group by dept_id,emp_job having sum(salary>1000);
三、select查詢(xún)之limit限制
1.查詢(xún)前三行數(shù)據(jù)
select * from employee limit 0,3;
2.查詢(xún)第三條到第七條數(shù)據(jù)
select * from employee limit 2,7;
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
MySQL數(shù)據(jù)遷移至達(dá)夢(mèng)數(shù)據(jù)庫(kù)的詳細(xì)教程
這篇文章主要為大家詳細(xì)介紹了MySQL數(shù)據(jù)遷移至達(dá)夢(mèng)數(shù)據(jù)庫(kù)的詳細(xì)教程,文中通過(guò)示例圖片進(jìn)行了詳細(xì)的介紹,有需要的小伙伴可以參考一下2025-03-03MYSQL中常用的強(qiáng)制性操作(例如強(qiáng)制索引)
對(duì)于經(jīng)常使用oracle的朋友可能知道,oracle的hint功能種類(lèi)很多,對(duì)于優(yōu)化sql語(yǔ)句提供了很多方法。同樣,在mysql里,也有類(lèi)似的hint功能。2014-05-05MYSQL ERROR 1045 (28000): Access denied for user (using pass
Mysql中添加用戶(hù)之后可能出現(xiàn)登錄時(shí)提示ERROR 1045 (28000): Access denied for user的錯(cuò)誤.2009-07-07使用limit,offset分頁(yè)場(chǎng)景時(shí)為什么會(huì)慢
這篇文章主要介紹了使用limit,offset分頁(yè)場(chǎng)景時(shí)為什么會(huì)慢,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11