其他分享
首页 > 其他分享> > DQL:查询语句

DQL:查询语句

作者:互联网

1.排序查询

首先建表:
CREATE TABLE student (
	NAME VARCHAR(20),
	id VARCHAR(12),
	sex CHAR,
	mathscore DOUBLE(4,1)
	);
	添加数据
INSERT INTO student VALUES("AA",1001,"男",99.0);
INSERT INTO student VALUES("BB",1002,"男",90.0);
INSERT INTO student VALUES("C",1003,"女",76.5);
INSERT INTO student VALUES("D",1005,"男",80.0);
INSERT INTO student VALUES("EEE",1004,"女",60.0);
语法:order by
order by 默认升序(ASC),降序为DESC.
例:SELECT * FROM student ORDER BY mathscore DESC , id ASC -- 如果成绩一样,就按学号排;

2.聚合函数

将一列数据作为一个整体,进行纵向的计算。
1.count:计算个数
	SELECT COUNT(sex = '男')FROM student;#不统计空的元素
		解决null值不统计问题(主键)
2.max  /  min 
	SELECT MAX(mathscore) FROM student;
3.sum:求和
	SELECT sum(mathscore) FROM student;
4.avg:求平均值
	SELECT AVG(mathscore) FROM student;

3.分组查询

group by分组字段
where在分组前限定,后不可以跟聚合函数(sum,avg,,,)
having在分组后限定,可以进行聚合函数的判断
1.#查询男,女生各数学的平均分,并统计学生个数
	SELECT sex ,COUNT(id), AVG(mathscore) FROM student GROUP BY sex;
2.在1,条件下分数低于70分的不参与评分
	SELECT sex,COUNT(id),AVG(mathscore) FROM student WHERE mathscore > 70 GROUP BY sex;
	![image](https://www.icode9.com/i/l/?n=22&i=blog/2871415/202206/2871415-20220602232735776-1970206741.png)

4.分页查询

limit分页
SELECT * FROM student LIMIT 0,3;#分页,从0开始,每页三项
SELECT * FROM student LIMIT 3,3;#第二页
开始的索引= (当前页码 -1) * 页码

标签:语句,INSERT,mathscore,查询,VALUES,student,DQL,sex,SELECT
来源: https://www.cnblogs.com/lutixiagit/p/16339351.html