mysql基础——数据表查询(全面解析)
目录
数据表查询
1.1 全列查询
1.2 指定列查询
1.3 查询字段为表达式
1.4 别名
1.5 去重DISTINCT
1.6 排序ORDER BY
1.6.1 升序和降序
1.6.2 使用表达式及别名排序
1.6.3 多个字段排序
1.7 条件查询:WHERE
1.7.1 基本查询:
1.7.2 AND与OR
1.7.3 BETWEEN ... AND ...
1.7.4 IN
1.7.5 模糊查询:LIKE
1.7.6 NULL 的查询:IS [NOT] NULL
1.7.7 分页查询:LIMIT
附本节测试SQL
数据表查询
查询语法如下:
SELECT column1, column2, ...
FROM table_name
WHERE condition
ORDER BY column1, column2, ...
LIMIT number_of_rows;/*
SELECT: 用于指定要检索的列
FROM: 指定要检索数据的表
WHERE: 可选,用于过滤要检索的数据行
ORDER BY: 可选,用于对结果集按指定列进行排序
LIMIT: 可选,用于限制结果集返回的行数
/*
实例:
-- 创建考试成绩表
DROP TABLE IF EXISTS exam_result;
CREATE TABLE exam_result (
id INT,
name VARCHAR(20),
chinese DECIMAL(3,1),
math DECIMAL(3,1),
english DECIMAL(3,1)
);-- 插⼊测试数据
INSERT INTO exam_result (id,name, chinese, math, english) VALUES
(1,'唐三藏', 67, 98, 56),
(2,'孙悟空', 87.5, 78, 77),
(3,'猪悟能', 88, 98.5, 90),
(4,'曹孟德', 82, 84, 67),
(5,'刘⽞德', 55.5, 85, 45),
(6,'孙权', 70, 73, 78.5),
(7,'宋公明', 75, 65, 30);
1.1 全列查询
select * from exam_result;
1.2 指定列查询
-- 指定列的顺序不需要按定义表的顺序来
SELECT id, name, english FROM exam_result;
1.3 查询字段为表达式
-- 表达式不包含字段
SELECT id, name, 10 FROM exam_result;
-- 表达式包含⼀个字段
SELECT id, name, english + 10 FROM exam_result;
-- 表达式包含多个字段
SELECT id, name, chinese + math + english FROM exam_result;
1.4 别名
为查询结果中的列指定别名,表示返回的结果集中,以别名作为该列的名称,语法:
SELECT column_name AS alias_name
FROM table_name;
/*
其中,column_name是要查询的字段名;
alias_name是要指定的别名;
table_name是要查询的表名。
*/
-- 结果集中,表头的列名=别名
SELECT id, name, chinese + math + english 总分 FROM exam_result;
1.5 去重DISTINCT
使用 DISTINCT 关键字对某列数据进行去重.
查询math列,可见98分重复了:
select math from exam_result;
使用DISTINCT进行去重,去重结果:
select distinct math from exam_result;
1.6 排序ORDER BY
语法:
SELECT column1, column2, ...
FROM table_name
WHERE condition
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...
/*
SELECT 用于选择要检索的列
FROM 指定要从中检索数据的表
WHERE 用于筛选数据行
ORDER BY 指定要按其进行排序的列。可以按一个或多个列进行排序,并可选择升序(ASC,默认)或降序(DESC)排序。
/*
1.6.1 升序和降序
-- 按数学成绩升序排序显⽰
SELECT *FROM exam_result ORDER BY math ASC;
-- 按数学成绩降序排序显⽰
SELECT *FROM exam_result ORDER BY math DESC;
1.6.2 使用表达式及别名排序
-- 查询同学及总分,由⾼到低SELECT name, chinese + english + math FROM exam_result
ORDER BY chinese + english + math DESC;
SELECT name, chinese + english + math total FROM exam_result
ORDER BY total DESC;
1.6.3 多个字段排序
-- 查询同学各⻔成绩,按数学降序,英语升序,语⽂升序的⽅式进⾏排序
select name,math,english,chinese from exam_result
order by math desc,english,chinese asc;
/*
当对多个字段进行排序时,排序优先级跟随书写顺序
*/
1.7 条件查询:WHERE
• WHERE 条件可以使用表达式,但不能使用别名。
• AND 的优先级高于OR ,在同时使用时,需要使用小括号 () 包裹优先执行的部分
1.7.1 基本查询:
-- 1、查询英语不及格的同学及英语成绩 ( < 60 )
SELECT name, english FROM exam_result WHERE english < 60;
-- 2、查询语⽂成绩好于英语成绩的同学
SELECT name, chinese, english FROM exam_result WHERE chinese > english;
-- 3、查询总分在 200 分以下的同学
SELECT name, chinese + math + english 总分 FROM exam_result
WHERE chinese + math + english < 200;
1.7.2 AND与OR
-- 查询语⽂成绩⼤于 80 分,且英语成绩⼤于 80 分的同学
SELECT * FROM exam_result WHERE chinese > 80 and english > 80;
-- 查询语⽂成绩⼤于 80 分,或英语成绩⼤于 80 分的同学
SELECT * FROM exam_result WHERE chinese > 80 or english > 80;
-- 观察 AND 和 OR 的优先级:
SELECT * FROM exam_result WHERE chinese > 80 or math > 70 and english > 70;
/*
执行顺序详解:
先执行 AND 条件:
对每条记录检查 math > 70 AND english > 70,只有同时满足这两个条件时,AND 表达式才为 true。
再执行 OR 条件:
将 chinese > 80 的结果与第一步的 AND 结果进行逻辑或(OR)。只要满足以下任一条件,整条记录就会被选中:
chinese > 80 为 true
math > 70 AND english > 70 为 true
等价于:
WHERE (chinese > 80) OR (math > 70 AND english > 70)
*/
SELECT * FROM exam_result WHERE (chinese > 80 or math > 70) and english > 70;
1.7.3 BETWEEN ... AND ...
-- 查询语⽂成绩在 [80, 90] 分的同学及语⽂成绩
SELECT name, chinese FROM exam_result WHERE chinese BETWEEN 80 AND 90;
-- 使⽤ AND 也可以实现
SELECT name, chinese FROM exam_result WHERE chinese >= 80 AND chinese<= 90;
1.7.4 IN
-- 使⽤ OR 也可以实现
SELECT name, math FROM exam_result WHERE math = 58 OR math = 59 OR math= 98 OR math = 99;
1.7.5 模糊查询:LIKE
-- % 匹配任意多个(包括 0 个)字符
SELECT name FROM exam_result WHERE name LIKE '孙%';-- 匹配到孙悟空、孙权 -- _ 匹配严格的⼀个任意字符
SELECT name FROM exam_result WHERE name LIKE '孙_';
1.7.6 NULL 的查询:IS [NOT] NULL
-- 查询 math 已知的同学姓名
SELECT name, math FROM exam_result WHERE math IS NOT NULL;-- 查询 math 未知的同学姓名
SELECT name, math FROM exam_result WHERE math IS NULL;
1.7.7 分页查询:LIMIT
语法:
-- 起始下标为 0
-- 从 0 开始,筛选 n 条结果
SELECT ... FROM table_name [ WHERE...] [ ORDER BY ...] LIMIT n;-- 从 s 开始,筛选 n 条结果
SELECT ... FROM table_name [ WHERE ...] [ ORDER BY...] LIMIT s, n;-- 从 s 开始,筛选 n 条结果,⽐第⼆种⽤法更明确,建议使⽤
SELECT ... FROM table_name [ WHERE ...] [ ORDER BY ...] LIMIT n OFFSET s;
案例:按 id 进行分页,每页 3 条记录,分别显示 第 1 、 2 、 3 页
附本节测试SQL
-- 创建考试成绩表
DROP TABLE IF EXISTS exam_result;
CREATE TABLE exam_result (
id INT,
name VARCHAR(20),
chinese DECIMAL(3,1),
math DECIMAL(3,1),
english DECIMAL(3,1)
);-- 插⼊测试数据
INSERT INTO exam_result (id,name, chinese, math, english) VALUES
(1,'唐三藏', 67, 98, 56),
(2,'孙悟空', 87.5, 78, 77),
(3,'猪悟能', 88, 98, 90),
(4,'曹孟德', 82, 84, 67),
(5,'刘⽞德', 55.5, 85, 45),
(6,'孙权', 70, 73, 78.5),
(7,'宋公明', 75, 65, 30);select * from exam_result;-- 指定列的顺序不需要按定义表的顺序来
SELECT id, name, english FROM exam_result;-- 表达式不包含字段
SELECT id, name, 10 FROM exam_result;
-- 表达式包含⼀个字段
SELECT id, name, english + 10 FROM exam_result;
-- 表达式包含多个字段
SELECT id, name, chinese + math + english FROM exam_result;-- 结果集中,表头的列名=别名
SELECT id, name, chinese + math + english AS 总分 FROM exam_result;select distinct math from exam_result;SELECT *FROM exam_result ORDER BY math ASC;SELECT name, chinese + english + math AS total FROM exam_result
ORDER BY total DESC;select name,math,english,chinese from exam_result
order by math desc,english,chinese asc;SELECT name, english FROM exam_result WHERE english < 60;SELECT name, chinese, english FROM exam_result WHERE chinese > english;-- 3、查询总分在 200 分以下的同学
SELECT name, chinese + math + english 总分 FROM exam_result
WHERE chinese + math + english < 200;SELECT * FROM exam_result WHERE chinese > 80 and english > 80;SELECT * FROM exam_result WHERE chinese > 80 or english > 80;-- 观察 AND 和 OR 的优先级:
SELECT * FROM exam_result WHERE chinese > 80 or (math > 70 and english > 70);
SELECT * FROM exam_result WHERE (chinese > 80 or math > 70) and english > 70;SELECT name, chinese FROM exam_result WHERE chinese BETWEEN 80 AND 90;SELECT name, chinese FROM exam_result WHERE chinese >= 80 AND chinese<= 90;SELECT name, math FROM exam_result WHERE math IN (58, 59, 98, 99);SELECT name, math FROM exam_result WHERE math = 58 OR math = 59 OR math= 98 OR math = 99;-- % 匹配任意多个(包括 0 个)字符
SELECT name FROM exam_result WHERE name LIKE '孙%';-- 匹配到孙悟空、孙权 -- _ 匹配严格的⼀个任意字符
SELECT name FROM exam_result WHERE name LIKE '孙_';1 -- 查询 math 已知的同学姓名
SELECT name, math FROM exam_result WHERE math IS NOT NULL;3 -- 查询 math 未知的同学姓名
SELECT name, math FROM exam_result WHERE math IS NULL;-- 第 1 页
SELECT id, name, math, english, chinese FROM exam_result ORDER BY id LIMIT 3 OFFSET 0;
-- 第 2 页
SELECT id, name, math, english, chinese FROM exam_result ORDER BY id LIMIT 3 OFFSET 3;
-- 第 3 页,如果结果不足 3 个,不会有影响
SELECT id, name, math, english, chinese FROM exam_result ORDER BY id LIMIT 3 OFFSET 6;