200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > MySQL单表查询看这一篇文章就够了

MySQL单表查询看这一篇文章就够了

时间:2022-03-18 22:26:03

相关推荐

MySQL单表查询看这一篇文章就够了

无条件简单查询方法

虚拟数据准备

-- [创建表] --

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", "-04-21", "1");

INSERT INTO `company_staff` VALUES ("2", "Roudo", "27", "男", "38000.000", "-02-21", "1");

INSERT INTO `company_staff` VALUES ("3", "Aaron", "30", "男", "26500.000", "-03-11", "1");

INSERT INTO `company_staff` VALUES ("4", "Lance", "48", "男", "46680.000", "-05-20", "1");

-- [国际事务] --

INSERT INTO `company_staff` VALUES ("5", "Leo", "23", "男", "13600.000", "-06-21", "2");

INSERT INTO `company_staff` VALUES ("6", "Lee", "24", "男", "12400.000", "-06-30", "2");

INSERT INTO `company_staff` VALUES ("7", "lvan", "26", "女", "12500.000", "-03-27", "2");

INSERT INTO `company_staff` VALUES ("8", "Jack", "29", "女", "22300.000", "-07-21", "2");

-- [法律部] --

INSERT INTO `company_staff` VALUES ("9", "张飞", "41", "女", "24000.000", "-05-21", "3");

INSERT INTO `company_staff` VALUES ("10", "李逵", "42", "女", "34000.000", "-04-15", "3");

INSERT INTO `company_staff` VALUES ("10", "罗翔", "45", "男", "54000.000", "-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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。