码迷,mamicode.com
首页 > 数据库 > 详细

【数据库】搜索语句

时间:2020-03-11 01:20:57      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:今天   查询   优先级   points   end   索引   span   总数   max   

今天用Koa2写了后端接口对数据库进行CRUD操作,补一些数据库相关的操作语句

SELECT * FROM <表名>

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   

原文地址:https://www.cnblogs.com/Mijiujs/p/12459910.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!