数据库常用函数,分组分页操作,及其limit关键字

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_39531549/article/details/82725941

 一、sql常用聚集函数

        1、sql语句中的聚集函数
		
		*where后面不能写聚集函数
		*关键字 having :
			-语句:having 聚集函数(字段); 
			-作用:当聚集函数需要写在条件位置时,需要使用having关键字

		*封装了一些固定的功能,可以在sql语句里面直接使用这个函数实现某些额功能
			
		=======================count函数===================================
		(1)*count:得道表中有多少条记录
			-语句:select count(*) from 表名 where 条件
			
			-练习:
			查询学生人数
			mysql> select count(*) as Stu from student;
			+-----+
			| Stu |
			+-----+
			|   9 |
			+-----+
			1 row in set (0.00 sec)

			查询数学成绩大于110的学生个数
			mysql> select count(*) from student where Math>110;
			+----------+
			| count(*) |
			+----------+
			|        3 |
			+----------+
			1 row in set (0.00 sec)

			查询总成绩大于350的学生个数
			mysql> select count(*) from student where English+Chinese+Math>350;
			+----------+
			| count(*) |
			+----------+
			|        3 |
			+----------+
			1 row in set (0.00 sec)

		=======================sum函数===================================
		(2)*sum:求和的操作
			-语句:select sum(求和字段) from 表名 where 条件

			-练习:
			统计一个班数学成绩
			mysql> select sum(Math) from student;
			+-----------+
			| sum(Math) |
			+-----------+
			|      1008 |
			+-----------+
			1 row in set (0.00 sec)

			统计语文 数学 外语各科总成绩
			mysql> select sum(Chinese),sum(Math),sum(English) from student;
			+--------------+-----------+--------------+
			| sum(Chinese) | sum(Math) | sum(English) |
			+--------------+-----------+--------------+
			|          980 |      1008 |          964 |
			+--------------+-----------+--------------+
			1 row in set (0.00 sec)

			统计班级语文成绩平均成绩
			mysql> select sum(Chinese)/count(*) from student;
			+-----------------------+
			| sum(Chinese)/count(*) |
			+-----------------------+
			|              108.8889 |
			+-----------------------+
			1 row in set (0.00 sec)

		=======================avg函数===================================
		*avg:计算平均数、
			-语句 : select avg(字段) from 表名;

			-练习:
				* 求一个班级数学平均分?
				mysql> select avg(Math) from student;
				+-----------+
				| avg(Math) |
				+-----------+
				|  112.0000 |
				+-----------+
				1 row in set (0.00 sec)

				* 求一个班级总分平均分
				mysql> select avg(Chinese+Math+English) from student;
				+---------------------------+
				| avg(Chinese+Math+English) |
				+---------------------------+
				|                  328.0000 |
				+---------------------------+
				1 row in set (0.00 sec)

		=======================max,min函数===================================
		*max:
			-语句 select max(字段) from 表名;
		*min:
			-语句 select min(字段) from 表名;

			-练习:
				 
				 计算数学的最高分和最低分
				mysql> select max(Math),min(Math) from student;
				+-----------+-----------+
				| max(Math) | min(Math) |
				+-----------+-----------+
				|       130 |       100 |
				+-----------+-----------+
				1 row in set (0.00 sec)

二、数据库分组操作group by关键字


	2、sql语句分组的操作
		-语句:group by 字段;

		-练习:
			
			以产品名称分组并计算总价
			mysql> select product,sum(piece) from orders group by product;
			+---------+------------+
			| product | sum(piece) |
			+---------+------------+
			| AV      |         90 |
			| CV      |       1400 |
			| DV      |          9 |
			| EV      |         90 |
			| MV      |        700 |
			| OV      |        100 |
			| TV      |        900 |
			+---------+------------+
			7 rows in set (0.00 sec)
			
			查询购买得到几类商品中,并且没类商品总价大于100的商品
			-------------使用关键字having---------------------
			语句:having 聚集函数(字段);

			-having关键字
				--在分组的基础上再进行条件的判断
				--group by product having sum(price)>100;
				--where后面不能写聚集函数,但是having后面可以写聚集函数

			mysql> select product,sum(piece) from orders group by product having sum(piece)>100 ;
			+---------+------------+
			| product | sum(piece) |
			+---------+------------+
			| CV      |       1400 |
			| MV      |        700 |
			| TV      |        900 |
			+---------+------------+
			3 rows in set (0.00 sec)+

 三、关键字limit分页操作及可视化工具sqlyog和关键字顺序

        3、sql语句里面关键字顺序
		
		***(重要)
			select...from...where...group by ...having...order by ....

			** S-F-W-G-H-O 组合

		=======================================================
			1、对数据库表中的记录插入,修改,删除操作
			2、对数据库表中记录的查询操作

	4、mysql可视化工具的使用
		* 通过cmd窗口,向表中插入中文记录,有乱码问题
			-解决方法:找到mysql的安装路径,找到一个文件my.ini
			-注意:如果要修改mysql里面的文件,首先备份
			-default-character-set=gbk

		* 可视化工具的使用
		-现在不建议使用这个工具,先通过cmd窗口把sql语句熟悉之后再使用这个工具
	
	5、mysql里面 limit 关键字
		*语句:limit 起始位置,个数;
			-这个关键字一般使用在分页功能里面

		* limit关键字不是标准sql的一部分,只能在mysql里面使用,在其他数据库里面不能使用
			-在oracle里面:使用 rownum关键字
			-在sqlserver里面:使用top关键字

			-练习:
				查询数据库表中前2条记录
				mysql> select * from orders limit 2 ;
				+----+---------+-------+
				| id | product | piece |
				+----+---------+-------+
				|  1 | TV      |   900 |
				|  2 | MV      |   100 |
				+----+---------+-------+
				2 rows in set (0.00 sec)
		
				查询表中3-5条记录
				mysql> select * from orders limit 2,3;
					-第一个 2 :从第几位开始 不包含当前位
					-第二个 3 :几条记录
				+----+---------+-------+
				| id | product | piece |
				+----+---------+-------+
				|  3 | AV      |    90 |
				|  4 | DV      |     9 |
				|  5 | EV      |    90 |
				+----+---------+-------+
				3 rows in set (0.00 sec)

猜你喜欢

转载自blog.csdn.net/weixin_39531549/article/details/82725941