时间:2021-07-01 10:21:17 帮助过:60人阅读
SELECT * FROM <表名> WHERE <条件表达式>
SELECT * FROM students WHERE id=1
SELECT * FROM students WHERE score<=80;
WHERE score >= 80 AND gender = ‘M‘;
WHERE score >= 80 OR gender = ‘M‘;
WHERE NOT class_id = 2; WHERE class_id <> 2;
优先级NOT AND OR
60~90
WHERE score >= 60 AND score <= 90
WHERE score BETWEEN 60 AND 90
投影查询
SELECT 列1, 列2, 列3 FROM <表名>
SELECT 列1 别名1, 列2 别名2, 列3 别名3 FROM<表名>
SELECT id, score points, name FROM students;
排序
SELECT * FROM students ORDER BY score; 默认ASC升序
SELECT * FROM students ORDER BY score DESC; 降序
SELECT * FROM students ORDER BY score DESC, gender;
分页(记录集索引为0)
LIMIT <M> OFFSET <N>
SELECT * FROM students LIMIT 3 OFFSET 0; 一页三个,第一页
SELECT * FROM students LIMIT 3 OFFSET 3; 一页三个,第二页
SELECT CEILING(COUNT(*) / 3) FROM students; 获取总页数
LIMIT:pageSize
OFFSET:pageSize*(pageIndex-1)
聚合查询:计算总数、合计值、平均值、最大值和最小值
获取数量SELECT COUNT(*) FROM students;
集的列名为num:SELECT COUNT(*) num FROM students;
某列总和SELECT SUM(score) FROM students
某列平均SELECT AVG(score) FROM students
某列最大SELECT MAX(score) FROM students
某列最大SELECT MIN(score) FROM students
分组聚合
分别搜索一班二班三班...的学生总数
SELECT COUNT(*) num FROM students GROUP BY class_id;
SELECT class_id, COUNT(*) num FROM students GROUP BY class_id;
统计各班的男生和女生人数:
SELECT class_id, gender, COUNT(*) num FROM students GROUP BY class_id, gender;
【数据库】搜索语句
标签:今天 查询 优先级 points end 索引 span 总数 max