mysql-07mysql單表查詢

一丶語法順序

  1. select
  2. from
  3. where
  4. group by

二丶執行順序

  1. from
  2. where
  3. group by
  4. select

實驗表準備

建立表

create table emp(
  id int not null unique auto_increment,
  name varchar(20) not null,
  sex enum('male','female') not null default 'male', #大部分是男的
  age int(3) unsigned not null default 28,
  hire_date date not null,
  post varchar(50),
  post_comment varchar(100),
  salary double(15,2),
  office int, #一個部門一個屋子
  depart_id int
);

添加表數據

#插入記錄
#三個部門:教學,銷售,運營
insert into emp(name,sex,age,hire_date,post,salary,office,depart_id) values
('jason','male',18,'20170301','張江第一帥形象代言',7300.33,401,1), #如下是教學部
('egon','male',78,'20150302','teacher',1000000.31,401,1),
('kevin','male',81,'20130305','teacher',8300,401,1),
('tank','male',73,'20140701','teacher',3500,401,1),
('owen','male',28,'20121101','teacher',2100,401,1),
('jerry','female',18,'20110211','teacher',9000,401,1),
('nick','male',18,'19000301','teacher',30000,401,1),
('sean','male',48,'20101111','teacher',10000,401,1),

('歪歪','female',48,'20150311','sale',3000.13,402,2),#如下是銷售部門
('丫丫','female',38,'20101101','sale',2000.35,402,2),
('丁丁','female',18,'20110312','sale',1000.37,402,2),
('星星','female',18,'20160513','sale',3000.29,402,2),
('格格','female',28,'20170127','sale',4000.33,402,2),

('張野','male',28,'20160311','operation',10000.13,403,3), #如下是運營部門
('程咬金','male',18,'19970312','operation',20000,403,3),
('程咬銀','female',18,'20130311','operation',19000,403,3),
('程咬銅','male',18,'20150411','operation',18000,403,3),
('程咬鐵','female',18,'20140512','operation',17000,403,3)
;
#ps:若是在windows系統中,插入中文字符,select的結果爲空白,能夠將全部字符編碼統一設置成gbk

三丶where約束條件

在你剛開始接觸mysql查詢的時候,建議你按照查詢的優先級順序拼寫出你的sql語句mysql

# 1.查詢id大於等於3小於等於6的數據
select * from emp where id>= 3 and id<= 6;
select * from emp where id between 3 and 6;

# 2.查詢薪資是20000或者18000或者17000的數據
select * from emp where salary = 20000 or salary = 18000 or salary = 17000;
select * from emp where salary in (20000, 18000, 17000);

# 3.查詢員工姓名中包含o字母的員工姓名和薪資
select * from emp where name like "%o%";

# 4.查詢員工姓名是由四個字符組成的員工姓名與其薪資
select * from emp where name like "____";

# 5.查詢id小於3或者大於6的數據
select * from emp where id<= 3 or id>= 6;
select * from emp where not id between 3 and 6;

# 6.查詢薪資不在20000,18000,17000範圍的數據
select * from emp where salary not in (20000,18000,17000);

# 7.查詢崗位描述爲空的員工名與崗位名  針對null不能用等號,只能用is
select name,post  from emp where post_comment is not NULL;

四丶group by

分組後可使用聚合函數sql

# 數據分組應用場景:每一個部門的平均薪資,男女比例等
# 1.按部門分組
select post from emp group by post;

# 2.獲取每一個部門的最高工資 
# 每一個部門的最高工資()
select post,max(salary) from emp group by post;

# 每一個部門的最低工資
select post,min(salary) from emp group by post;

# 每一個部門的平均工資
select post,avg(salary) from emp group by post;

# 每一個部門的工資總和
select post,sum(salary) from emp group by post;

# 每一個部門的人數
select post,count(id) from emp group by post;

# 3.查詢分組以後的部門名稱和每一個部門下全部的成員姓名
select post,group_concat(name) from emp group by post;

# group_concat(分組以後用)不只能夠用來顯示除分組外字段還有拼接字符串的做用
select post, group_concat("教師:",name) from emp group by post;

# 4.補充concat(不分組時用)拼接字符串達到更好的顯示效果 as語法使用
select concat("薪資:",salary),concat("姓名:",name) from emp where salary >= 5000;

# 補充as語法 便可以給字段起別名也能夠給表起
select concat("薪資:",salary) as "薪資",concat("姓名:",name) as "姓名" from emp where salary >= 5000;

# 查詢四則運算
# 查詢每一個人的年薪
select concat("姓名:",name) as "姓名",concat("薪資:",salary*12) as "年薪" from emp where salary >= 5000;

五丶having

having的語法格式與where一致,只不過having是在分組以後進行的過濾,即where雖然不能用聚合函數,可是having能夠!windows

強調:having必須在group by後面使用!!!函數

實驗表創建

select post,avg(salary) from emp2
        where age >= 30
        group by post
        having avg(salary) > 10000;


#強調:having必須在group by後面使用
select * from emp having avg(salary) > 10000;  # 報錯

六丶distinct

對有重複的展現數據進行去重操做post

select distinct post from emp;

七丶order by

排序編碼

asc(默認) 升序spa

desc 降序code

select * from emp order by salary asc; #默認升序排
select * from emp order by salary desc; #降序排

select * from emp order by age desc; #降序排

#先按照age降序排,在年輕相同的狀況下再按照薪資升序排
select * from emp order by age desc,salary asc; 

# 統計各部門年齡在10歲以上的員工平均工資,而且保留平均工資大於1000的部門,而後對平均工資進行排序
select post,avg(salary) from emp
    where age > 10
    group by post
    having avg(salary) > 1000
    order by avg(salary)
    ;

八丶limit

# 限制展現條數
select * from emp limit 3;
# 查詢工資最高的人的詳細信息
select * from emp order by salary desc limit 1;

# 分頁顯示
select * from emp limit 0,5;  # 第一個參數表示起始位置,第二個參數表示的是條數,不是索引位置
select * from emp limit 5,5;

九丶正則

select * from emp where name regexp '^j.*(n|y)$';	#這邊的正則解讀爲以j開頭,以y或者n結尾,中間儘量匹配更多的字符
相關文章
相關標籤/搜索