06、單表查詢
阿新 • • 發佈:2022-05-12
一、單表查詢的語法
select 欄位1,欄位2,... from 表名
where 條件
group by field
having 篩選
order by feild
limit 限制條數
二、關鍵字的執行優先順序(重點)
# 重點中的重點:關鍵字的執行優先順序
from
where
group by
having
select
distinct
order by
limit
regexp 正則表示式
# 解釋
1.找到表: from
2.拿到 where 指定的約束條件,去檔案/表中取出一條條記錄
3.將取出的一條條記錄進行分組 group by ,如果沒有 group by ,則整體為一組
4.將分組的結果進行 having 過濾
5.執行 select
6.去重
7.將結果按條件排序: order by
8.限制結果的顯示條數
三、簡單查詢
1、單表查詢準備
create table emp(
id int primary key 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), #以下是教學部
('tom','male',78,'20150302','teacher',1000000.31,401,1),
('kevin','male',81,'20130305','teacher',8300,401,1),
('tony','male',73,'20140701','teacher',3500,401,1),
('owen','male',28,'20121101','teacher',2100,401,1),
('jack','female',18,'20110211','teacher',9000,401,1),
('jenny','male',18,'19000301','teacher',30000,401,1),
('sank','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);
2、查詢關鍵字
select
控制查詢表中的哪些欄位對應的資料
from
控制查詢的表
3、查詢關鍵字之where
where字句中可以使用:
1. 比較運算子:> < >= <= <> !=
2. between 80 and 100 值在10到20之間
3. in(80,90,100) 值是10或20或30
4. like '%條件%'
pattern可以是%或_,
% 表示任意多字元
_ 表示一個字元
5. 邏輯運算子:在多個條件直接可以使用邏輯運算子 and or not
# where其實就是對資料進行篩選
# 1.查詢id大於等於3小於等於6的資料
select * from emp where id >= 3 and id <= 6; # 比較運算子查詢
select * from emp where id between 3 and 6; # 關鍵字: between and 查詢
# 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); # 關鍵字 in 查詢
"""
模糊查詢
關鍵字 like
關鍵符號
%:匹配任意個數的任意字元
_:匹配單個個數的任意字元
"""
# 3.查詢員工姓名中包含o字母的員工姓名和薪資
select name,salary from emp where name like '%o%'; # 模糊查詢
# 4.查詢員工姓名為四個字元組成的員工姓名和薪資
select name,salary from emp where name like '____'; # 模糊查詢
select name,salary from emp where char_length(name) = 4; # 比較運算子
# 5.查詢id小於3或者大於6的資料
select * from emp where id not between 3 and 6; # not 為非
# 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 NULL;
select name,post from emp where post_comment is not NULL;
4、查詢關鍵字之group by分組
分組
按照某個指定的條件將單個單個的資料分為一個個整體
應用場景
eg:
求每個部門的平均薪資
求每個國家的人均GDP
求男女平均薪資
如何對資料進行分組
關鍵字 group by 條件
"""
分組之後不再以單個個體為研究物件 也無法直接再獲取單個個體的資料
研究物件應該是分組的整體
分組之後預設只能直接獲取到分組的依據 其他欄位資料無法直接獲取
如果需要實現上述要求 還是修改sql_mode
set global sql_mode='only_full_group_by';
"""
# 首先明確一點:分組發生在where之後,即分組是基於where之後得到的記錄而進行的
# 小竅門:‘每’這個字後面的欄位,就是我們分組的依據
1.獲取每個部門的最大薪資
select post,max(salary) from emp group by post;
2.統計每個部門的人數
select post,count(id) from emp group by post;
3.獲取每個部門的員工姓名
select post,group_concat(name,'|',salary) from emp group by post;
# group_concat用於分組之後獲取分組以外的欄位資料並支援拼接
# concat用於分組之前的拼接操作
select id,concat(name,'|',salary) as '嘿嘿' from emp;
# concat_ws當多個欄位連線符相同的情況下推薦使用
select id,concat_ws('|',name,sex,salary,age) from emp;
"""
在檢視結果的時候可以給欄位起別名
select post as '部門',max(salary) as '最高薪資' from emp group by post;
as可以省略但是為了語義更加明確建議不要省略
"""
5、聚合函式
max()
min()
sum()
count()
avg()
# 上述聚合函式都是在分組之後使用 用於操作整體資料
1. 查詢崗位名以及各崗位內包含的員工個數
select post,count(id) from emp group by post;
2. 查詢公司內男員工和女員工的個數
select sex,count(id) from emp group by sex;
3. 查詢崗位名以及各崗位的平均薪資
select post,avg(salary) from emp group by post;
4. 查詢崗位名以及各崗位的最高薪資
select post,max(salary) from emp group by post;
5. 查詢崗位名以及各崗位的最低薪資
select post,min(salary) from emp group by post;
6. 查詢男員工與男員工的平均薪資,女員工與女員工的平均薪資
select sex,avg(salary) from emp group by sex;
6、查詢關鍵字之having過濾
where與having都是用來篩選資料的 但是where用於分組之前的篩選、having用於分組之後的篩選 為了人為的區分開 我們將where用篩選來形容 having用過濾來形容 # 執行優先順序從高到低:where > group by > having # Where 發生在分組group by之前,因而Where中可以有任意欄位,但是絕對不能使用聚合函式。 # Having發生在分組group by之後,因而Having中可以使用分組的欄位,無法直接取到其他欄位,可以使用聚合函式 # 統計各部門年齡在30歲以上的員工平均工資,並且保留平均工資大於10000的部門 mysql> select post,avg(salary) from emp where age>30 group by post having avg(salary)>10000; +---------+---------------+ | post | avg(salary) | +---------+---------------+ | teacher | 255450.077500 | +---------+---------------+ """ 將一個複雜的查詢題拆分成多個簡單的小題 """
7、查詢關鍵字之distinct去重
""" 去重的前提示是存在一模一樣的資料 如果存在主鍵肯定無法去重 """ # 對有重複的展示資料進行去重操作 一定要是重複的資料 select distinct id,age from emp; select distinct post from emp;
8、查詢關鍵字之order by排序
order by預設是升序 預設的關鍵字是asc # ascend select * from emp order by salary asc; 也可以改為降序 desc # descend select * from emp order by salary desc; # order by排序支援多個欄位組合(第一個不行 就往後繼續排) select * from emp order by age,salary; select * from emp order by age asc,salary desc;
9、查詢關鍵字之 limit 限制查詢
# 工資降序排序,並限制前三位的工資 select * from emp order by salary desc limit 3; #預設初始位置為0 select * from emp order by salary desc limit 3,3; #從第3開始,即先查詢出第4條,然後包含這一條在內往後查3條
10、使用正則表示式 regexp 查詢
select * from emp where name regexp '^j.*(n|y)$';
11、exists 是否存在
EXISTS關字鍵字表示存在。在使用EXISTS關鍵字時,內層查詢語句不返回查詢的記錄, 而是返回一個真假值,True或False。 當返回True時,外層查詢語句將進行查詢 當返回值為False時,外層查詢語句不進行查詢。 select * from emp where exists (select id from emp where id > 3); select * from emp where exists (select id from emp where id > 250);
補充:操作表的SQL語句補充
# 1.修改表名稱 alter table t1 rename t2; rename table t1 to t123; # 關鍵字修改 rename table t2 to t20, t3 to t30, t4 to t40; # 2.新增表字段 alter table t9 add pwd int; alter table t9 add hobby varchar(32) after age; alter table t9 add nid int first; # 3.刪除表字段 alter table t9 drop nid; # 4.修改欄位名和欄位型別 alter table t9 modify age tinyint; # modify只能修改欄位型別 alter table t9 change pwd password int; # change欄位名和型別
複製表(瞭解)
# 查詢語句執行的結果也是一張表,可以看成虛擬表 # 複製表結構+記錄 (key不會複製: 主鍵、外來鍵和索引) create table new_service select * from service; # 只拷貝表結構(不包含鍵) create table new1_service select * from service where 1=2; # 拷貝結構包含各種key create table t4 like employees;