無條件簡單查詢方法
虛擬數據準備
-- [創建表] --
DROP TABLE IF EXISTS `company_staff`;
CREATE TABLE `company_staff` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(125) NOT NULL,
`age` tinyint(4) DEFAULT '0',
`sex` enum('男','女','保密') NOT NULL DEFAULT '保密',
`salary` decimal(10,3) NOT NULL DEFAULT '5000.000',
`hire_date` date NOT NULL,
`dept_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;
-- [創建數據] --
-- [軟件開發部] --
INSERT INTO `company_staff` VALUES ('1', 'Tom', '25', '男', '27000.000', '2019-04-21', '1');
INSERT INTO `company_staff` VALUES ('2', 'Roudo', '27', '男', '38000.000', '2020-02-21', '1');
INSERT INTO `company_staff` VALUES ('3', 'Aaron', '30', '男', '26500.000', '2017-03-11', '1');
INSERT INTO `company_staff` VALUES ('4', 'Lance', '48', '男', '46680.000', '2003-05-20', '1');
-- [國際事務] --
INSERT INTO `company_staff` VALUES ('5', 'Leo', '23', '男', '13600.000', '2017-06-21', '2');
INSERT INTO `company_staff` VALUES ('6', 'Lee', '24', '男', '12400.000', '2013-06-30', '2');
INSERT INTO `company_staff` VALUES ('7', 'lvan', '26', '女', '12500.000', '2014-03-27', '2');
INSERT INTO `company_staff` VALUES ('8', 'Jack', '29', '女', '22300.000', '2003-07-21', '2');
-- [法律部] --
INSERT INTO `company_staff` VALUES ('9', '張飛', '41', '女', '24000.000', '2003-05-21', '3');
INSERT INTO `company_staff` VALUES ('10', '李逵', '42', '女', '34000.000', '2005-04-15', '3');
INSERT INTO `company_staff` VALUES ('10', '羅翔', '45', '男', '54000.000', '2005-03-15', '3');
-- [銷售部] --
INSERT INTO `company_staff` VALUES ('11', '孫權', '37', '女', '65000.000', '2000-06-21', '4');
簡單查詢方法格式
#查詢語法:
select [distinct]*(所有)|字段名,...字段名 from 表名;
#查詢所有字段信息
select * from company_staff;
#查詢指定字段信息
select id,name from company_staff;
#別名查詢,使用的as關鍵字,as可以省略的
select name,age as'年齡',salary '工資' from company_staff;
#刪除重復查詢--[distinct]
select distinct age from company_staff;

按條件查詢方法
條件查詢:使用 WHERE 關鍵字 對簡單查詢的結果集 進行過濾
- 比較運算符: > < >= <= = <>(!=)
- null 關鍵字: is null , not null
- 邏輯運算符: 與 and 或 or (多個條件時,需要使用邏輯運算符進行連接)
#查詢格式:
select [distinct]*(所有)|字段名,...字段名 from 表名 [where 條件過濾]
#比較運算符: > < >= <= = <>(!=) is null 是否為null
select * from company_staff where age = 33;
select * from company_staff where age < 23;
select * from company_staff where age is null;
select * from company_staff where age is not null;
#邏輯運算符: 與 and 或 or
select * from company_staff where age = 23 and salary =29000;
select * from company_staff where age = 35 or salary =29000;
按照區間查詢方法
關鍵字 between 1 and 5 :表示 獲得1 到 5 區間的內容
# 應用 between...and 進行區間 查詢
select * from company_staff where salary between 14000 and 23000;
# PS: between...and 前后包含所指定的值
等價于 select * from company_staff where salary >= 14000 and salary <= 23000;
按照集合查詢方法
關鍵字: in, not null
#使用 in 集合(多個字段)查詢
select * from company_staff where age in(23,29,41);
等價于: select * from company_staff where age =23 or age = 29 or age =41;
#使用 in 集合 排除指定值查詢
select * from company_staff where age not in(23,29,41);

應用模糊查詢
關鍵字 like , not like
%: 任意多個字符
_ : 只能是單個字符
#模糊查詢 like %:任意多個字符, _:單個字符
#查詢姓名以"L"字開頭的
select * from company_staff where name like 'L%';
#查詢姓名以"L"字結尾的
select * from company_staff where name like '%L';
#查詢姓名中含有"L"字的
select * from company_staff where name like '%L%';
#查詢 name 名稱 是三個字符的人
select * from company_staff where name like '___';
#查詢 name 名稱 的第二個字符是 'o'的人
select * from company_staff where name like '_o%';
#排除名字帶 a的人
select * from company_staff where name not like 'a%'
應用排序查詢
關鍵字: ORDER BY 字段1 DESC, 字段2 ASC
#排序查詢格式:
select 字段|* from 表名 [where 條件過濾] [order by 字段[ASC][DESC]]
升序:ASC 默認為升序
降序:DESC
PS:排序order by 要寫在select語句末尾
#按人員工資正序排列,注意:此處可以省略 ASC關鍵字
select * from company_staff order by salary ASC;
select * from company_staff order by salary;
#工資大于12000的人,按工資倒序排列
select * from company_staff where salary >12000 order by salary DESC;
#按中文排序
select * from company_staff order by name;
#強制中文排序
select * from company_staff order by CONVERT(name USING gbk);
ps:UTF8 默認校對集是 utf8_general_ci , 它不是按照中文來的。你需要強制讓MySQL按中文來排序
聚合函數匯總
聚合: 將分散的聚集到一起.
聚合函數: 對列進行操作,返回的結果是一個單一的值,除了 COUNT 以外,都會忽略空值
- COUNT:統計指定列不為NULL的記錄行數;
- SUM:計算指定列的數值和,如果指定列類型不是數值類型,那么計算結果為0;
- MAX:計算指定列的最大值,如果指定列是字符串類型,那么使用字符串排序運算;
- MIN:計算指定列的最小值,如果指定列是字符串類型,那么使用字符串排序運算;
- AVG:計算指定列的平均值,如果指定列類型不是數值類型,那么計算結果為0;
#格式:
select 聚合函數(字段) from 表名;
#統計人員中最大年齡、最小年齡,平均年齡分別是多少
select max(age),min(age),avg(age) from company_staff;
分組查詢方法
分組的含義: 將一些具有相同特征的數據 進行歸類.比如:性別,部門,崗位等等
怎么區分什么時候需要分組呢?
套路: 遇到 "每" 字,一般需要進行分組操作.
例如: 1. 公司每個部門有多少人.
2. 公司中有 多少男員工 和 多少女員工.
#分組查詢格式:
select 被分組的字段 from 表名 group by 分組字段 [having 條件字段]
ps: 分組查詢可以與 聚合函數 組合使用.
#查詢每個部門的平均工資
select avg(salary),dept from company_staff GROUP BY dept;
#查詢每個部門的平均薪資 并且看看這個部門的員工都有誰?
select avg(salary),dept,GROUP_CONCAT(name) from company_staff GROUP BY dept;
#GROUP_CONCAT(expr):按照分組,將expr字符串按逗號分隔,組合起來
#查詢平均薪資大于15000的部門, 并且看看這個部門的員工都有誰?
select avg(salary),dept,GROUP_CONCAT(name) from company_staff GROUP BY dept; having avg(salary)>10000;
where 與 having區別
執行優先級從高到低:where > group by > having
Where 發生在分組group by之前,因而Where中可以有任意字段,但是絕對不能使用聚合函數。
Having發生在分組group by之后,因而Having中可以使用分組的字段,無法直接取到其他字段,可以使用聚合函數
分頁查詢方法
好處:限制查詢數據條數,提高查詢效率
#查詢前2條數據
select * from company_staff limit 2;
#查詢第3條到第7條數據
select * from company_staff limit 3,7;
#查詢第10條到第15條數據
select * from company_staff limit 10,5;
ps: limit (起始條數),(查詢多少條數);
正則表達式方法
MySQL中使用 REGEXP 操作符來進行正則表達式匹配。

# ^ 匹配 name 名稱 以 "L" 開頭的數據
select * from company_staff where name REGEXP '^L';
# $ 匹配 name 名稱 以 "m" 結尾的數據
select * from company_staff where name REGEXP 'm$';
# . 匹配 name 名稱 第二位后包含"f"的人員 "."表示任意字符
select * from company_staff where name REGEXP '.f';
# [abci] 匹配 name 名稱中含有指定集合內容的人員
select * from company_staff where name REGEXP '[lmns]';
# [^Tom] 匹配 不符合集合中條件的內容 , ^表示取反
select * from company_staff where name REGEXP '[^Tom]';
#注意1:^只有在[]內才是取反的意思,在別的地方都是表示開始處匹配
#注意2 : 簡單理解 name REGEXP '[^Tom]' 等價于 name != 'Tom'
# 'l|s' 匹配 條件中的任意值
select * from company_staff where name REGEXP 'l|s';
#查詢以L開頭以s結尾的數據
select * from company_staff where name regexp '^L.*s$';
#注意:^w 表示w開頭, .*表示中間可以有任意多個字符, i$表示以 i結尾
SQL 語句關鍵字的執行順序
查詢:姓名不同人員的最高工資,并且要求大于12500元,同時按最高工資進行排序并取出前3條.
select name, max(salary)
from company_staff
where name is not null
group by name
havng max(salary) > 12500
order by max(salary)
limit 0,3
在上面的示例中 SQL 語句的執行順序如下
(1). 首先執行 FROM 子句, 從 company_staff 表 組裝數據源的數據
(2). 執行 WHERE 子句, 篩選 company_staff 表中 name 不為 NULL 的數據
(3). 執行 GROUP BY 子句, 把 company_staff 表按 "name" 列進行分組
(4). 計算 max() 聚集函數, 按 "工資" 求出工資中最大的一些數值
(5). 執行 HAVING 子句, 篩選工資大于 12500的人員.
(7). 執行 ORDER BY 子句, 把最后的結果按 "Max 工資" 進行排序.
(8). 最后執行 LIMIT 子句, . 進行分頁查詢
執行順序: FROM -> WHERE -> GROUP BY -> HAVING -> SELECT -> ORDER BY ->limit