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);
准備表和數據

 

#查詢語法:  
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 關鍵字 對簡單查詢的結果集 進行過濾

  1. 比較運算符: > < >= <= = <>(!=)

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

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

#查詢格式:
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 區間的內容

# 使用  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 null

#使用 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 like

    %:  任意多個字符

       _  : 只能是單個字符

#模糊查詢  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 ASC

#排序查詢格式:
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;

#格式:
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中可以使用分組的字段,無法直接取到其他字段,可以使用聚合函數

九 分頁查詢 

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

#查詢前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"。

 

# ^  匹配 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結尾

正則詳情參考 :http://www.cnblogs.com/wangfengming/articles/8067037.html

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

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

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  

十二 練習題

練習題一: http://www.cnblogs.com/wangfengming/articles/7944029.html

練習題二: http://www.cnblogs.com/wangfengming/articles/7889786.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM