MySQL之單表查詢

一.簡單查詢

複製代碼
-- 建立表 DROP TABLE IF EXISTS `person`; CREATE TABLE `person` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL, `age` tinyint(4) DEFAULT '0', `sex` enum('','','人妖') NOT NULL DEFAULT '人妖', `salary` decimal(10,2) NOT NULL DEFAULT '250.00', `hire_date` date NOT NULL, `dept_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8; -- 建立數據 -- 教學部 INSERT INTO `person` VALUES ('1', 'alex', '28', '人妖', '53000.00', '2010-06-21', '1'); INSERT INTO `person` VALUES ('2', 'wupeiqi', '23', '', '8000.00', '2011-02-21', '1'); INSERT INTO `person` VALUES ('3', 'egon', '30', '', '6500.00', '2015-06-21', '1'); INSERT INTO `person` VALUES ('4', 'jingnvshen', '18', '', '6680.00', '2014-06-21', '1'); -- 銷售部 INSERT INTO `person` VALUES ('5', '歪歪', '20', '', '3000.00', '2015-02-21', '2'); INSERT INTO `person` VALUES ('6', '星星', '20', '女', '2000.00', '2018-01-30', '2'); INSERT INTO `person` VALUES ('7', '格格', '20', '女', '2000.00', '2018-02-27', '2'); INSERT INTO `person` VALUES ('8', '週週', '20', '女', '2000.00', '2015-06-21', '2'); -- 市場部 INSERT INTO `person` VALUES ('9', '月月', '21', '女', '4000.00', '2014-07-21', '3'); INSERT INTO `person` VALUES ('10', '安琪', '22', '女', '4000.00', '2015-07-15', '3'); -- 人事部 INSERT INTO `person` VALUES ('11', '周明月', '17', '女', '5000.00', '2014-06-21', '4'); -- 鼓勵部 INSERT INTO `person` VALUES ('12', '蒼老師', '33', '女', '1000000.00', '2018-02-21', null);
複製代碼

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#查詢語法: 
select [distinct]*(全部)|字段名,...字段名 from 表名;
 
#查詢全部字段信息
select * from person;
 
#查詢指定字段信息
select id,name,age,sex,salary from person;
 
#別名查詢,使用的as關鍵字,as能夠省略的
select name,age as '年齡' ,salary '工資' from person;
 
#直接對列進行運算,查詢出全部人工資,並每人增長100塊.
select (5/2);
select name, salary+100 from person;
 
#剔除重複查詢
select distinct age from person;

二  條件查詢

  條件查詢:使用 WHERE 關鍵字 對簡單查詢的結果集 進行過濾html

  1. 比較運算符: > < >= <= = <>(!=)正則表達式

    2. null 關鍵字: is null , not nullsql

    3.邏輯運算符: 與 and 或 or (多個條件時,須要使用邏輯運算符進行鏈接)ide

1
2
3
4
5
6
7
8
9
10
11
12
#查詢格式:
select [distinct]*(全部)|字段名,...字段名 from 表名 [ where 條件過濾]
 
#比較運算符: > < >= <= = <>(!=)    is null 是否爲null
select * from person where age = 23;
select * from person where age <> 23;
select * from person where age is null ;
select * from person where age is not null ;
 
#邏輯運算符: 與 and 或 or
select * from person where age = 23 and salary =29000;
select * from person where age = 23 or salary =29000;

三 區間查詢

關鍵字 between 10 and  20 :表示 得到10 到 20 區間的內容函數

1
2
3
4
# 使用  between...and  進行區間 查詢
select * from person where salary between 4000 and 8000;
ps: between...and 先後包含所指定的值
等價於 select * from person where salary >= 4000 and salary <= 8000;

四 集合查詢

 關鍵字: in, not nullspa

1
2
3
4
5
6
#使用 in 集合(多個字段)查詢
select * from person where age in (23,32,18);
等價於: select * from person where  age =23 or age = 32 or age =18;
 
#使用 in 集合 排除指定值查詢
select * from person where age not in (23,32,18);

五 模糊查詢

  關鍵字 like , not likecode

    %:  任意多個字符regexp

       _  : 只能是單個字符htm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#模糊查詢  like %:任意多個字符,  _:單個字符
 
#查詢姓名以 "張" 字開頭的
select * from person where name like '張%' ;
#查詢姓名以 "張" 字結尾的
select * from person where name like '%張' ;
#查詢姓名中含有 "張" 字的
select * from person where name like '%張%' ;
 
#查詢 name 名稱 是四個字符的人
select * from person where name like '____' ;
#查詢 name 名稱 的第二個字符是 'l' 的人
select * from person where name like '_l%' ;
 
#排除名字帶 a的學生
select * from student where name not like 'a%'

 

六 排序查詢

關鍵字: ORDER BY  字段1 DESC, 字段2 ASCblog

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#排序查詢格式:
select 字段|* from 表名 [ where 條件過濾] [order by 字段[ASC][DESC]]
 
升序:ASC 默認爲升序
降序:DESC
PS:排序order by 要寫在 select 語句末尾
 
#按人員工資正序排列,注意:此處能夠省略 ASC關鍵字
select * from person order by salary ASC;
select * from person order by salary;
 
#工資大於5000的人,按工資倒序排列
select * from person where salary >5000 order by salary DESC;
 
#按中文排序
select * from person order by name;
 
#強制中文排序
select * from person order by CONVERT(name USING gbk);
ps:UTF8 默認校對集是 utf8_general_ci , 它不是按照中文來的。你須要強制讓MySQL按中文來排序

 

七 聚合函數

  聚合:  將分散的彙集到一塊兒.
  聚合函數: 對列進行操做,返回的結果是一個單一的值,除了 COUNT 之外,都會忽略空值

  COUNT:統計指定列不爲NULL的記錄行數;
  SUM:計算指定列的數值和,若是指定列類型不是數值類型,那麼計算結果爲0;
  MAX:計算指定列的最大值,若是指定列是字符串類型,那麼使用字符串排序運算;
  MIN:計算指定列的最小值,若是指定列是字符串類型,那麼使用字符串排序運算;
  AVG:計算指定列的平均值,若是指定列類型不是數值類型,那麼計算結果爲0;

1
2
3
4
5
#格式:
select 聚合函數(字段) from 表名;
 
#統計人員中最大年齡、最小年齡,平均年齡分別是多少
select max(age),min(age),avg(age) from person;

八 分組查詢

 分組的含義: 將一些具備相同特徵的數據 進行歸類.好比:性別,部門,崗位等等

 怎麼區分何時須要分組呢?  

  套路: 遇到 "每" 字,通常須要進行分組操做.

  例如: 1. 公司每一個部門有多少人.

      2. 公司中有 多少男員工 和 多少女員工.

複製代碼
#分組查詢格式:
select 被分組的字段 from 表名 group by 分組字段 [having 條件字段] ps: 分組查詢能夠與 聚合函數 組合使用. #查詢每一個部門的平均薪資 select avg(salary),dept from person GROUP BY dept; #查詢每一個部門的平均薪資 而且看看這個部門的員工都有誰? select avg(salary),dept,GROUP_CONCAT(name) from person GROUP BY dept; #GROUP_CONCAT(expr):按照分組,將expr字符串按逗號分隔,組合起來 #查詢平均薪資大於10000的部門, 而且看看這個部門的員工都有誰? select avg(salary),dept,GROUP_CONCAT(name) from person GROUP BY dept; having avg(salary)>10000;
複製代碼

 where 與 having區別:
#執行優先級從高到低:where > group by > having
#1. Where 發生在分組group by以前,於是Where中能夠有任意字段,可是絕對不能使用聚合函數。
#2. Having發生在分組group by以後,於是Having中能夠使用分組的字段,沒法直接取到其餘字段,能夠使用聚合函數

九 分頁查詢 

 好處:限制查詢數據條數,提升查詢效率

1
2
3
4
5
6
7
8
9
10
#查詢前5條數據
select * from person limit 5;
 
#查詢第5條到第10條數據
select * from person limit 5,5;
 
#查詢第10條到第15條數據
select * from person limit 10,5;
 
ps: limit (起始條數),(查詢多少條數);

 十 正則表達式  

 MySQL中使用 REGEXP 操做符來進行正則表達式匹配。

模式 描述
^ 匹配輸入字符串的開始位置。 
$ 匹配輸入字符串的結束位置。
. 匹配任何字符(包括回車和新行)
[...] 字符集合。匹配所包含的任意一個字符。例如, '[abc]' 能夠匹配 "plain" 中的 'a'。
[^...] 負值字符集合。匹配未包含的任意字符。例如, '[^abc]' 能夠匹配 "plain" 中的'p'。
p1|p2|p3 匹配 p1 或 p2 或 p3。例如,'z|food' 能匹配 "z" 或 "food"。'(z|f)ood' 則匹配 "zood" 或 "food"。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# ^  匹配 name 名稱 以 "e" 開頭的數據
select * from person where name REGEXP '^e' ;
 
# $  匹配 name 名稱 以 "n" 結尾的數據
select * from person where name REGEXP 'n$' ;
 
# . 匹配 name 名稱 第二位後包含"x"的人員 "."表示任意字符
select * from person where name REGEXP '.x' ;
 
# [abci] 匹配 name 名稱中含有指定集合內容的人員
select * from person where name REGEXP '[abci]' ;
 
# [^alex] 匹配 不符合集合中條件的內容 , ^表示取反
select * from person where name REGEXP '[^alex]' ;
#注意1:^只有在[]內纔是取反的意思,在別的地方都是表示開始處匹配
#注意2 : 簡單理解 name  REGEXP '[^alex]' 等價於 name != 'alex'
 
# 'a|x' 匹配 條件中的任意值
select * from person where name REGEXP 'a|x' ;  
 
#查詢以w開頭以i結尾的數據
select * from person where name regexp '^w.*i$' ;
#注意:^w 表示w開頭, .*表示中間能夠有任意多個字符, i$表示以 i結尾

正則詳情參考 :https://www.cnblogs.com/zhaopanpan/p/10133224.html

十一 SQL 語句關鍵字的執行順序

查詢:姓名不一樣人員的最高工資,而且要求大於5000元,同時按最大工資進行排序並取出前5條.

1
2
3
4
5
6
7
8
9
10
11
12
13
select name, max(salary)
   
from person  
   
where name is not null  
   
group by name  
   
having max(salary) > 5000
   
order by max(salary)
 
limit 0,5

在上面的示例中 SQL 語句的執行順序以下:

   (1). 首先執行 FROM 子句, 從 person 表 組裝數據源的數據

   (2). 執行 WHERE 子句, 篩選 person 表中 name 不爲 NULL 的數據

   (3). 執行 GROUP BY 子句, 把 person 表按 "name" 列進行分組

   (4). 計算 max() 彙集函數, 按 "工資" 求出工資中最大的一些數值

   (5). 執行 HAVING 子句, 篩選工資大於 5000的人員.

   (7). 執行 ORDER BY 子句, 把最後的結果按 "Max 工資" 進行排序.

   (8). 最後執行 LIMIT 子句, . 進行分頁查詢

執行順序: FROM -> WHERE -> GROUP BY -> HAVING -> SELECT -> ORDER BY ->limit

相關文章
相關標籤/搜索