MySQL学习2--数据查询
mysql> select * from person; +------+--------+------+------------+--------+---------------------+ | id | name | age | salary | gender | regTime | +------+--------+------+------------+--------+---------------------+ | 3 | 盖伦 | 78 | 2637363.45 | 0 | 2023-06-28 15:31:31 | | 4 | 艾希 | 25 | 900.00 | 0 | 2023-06-28 16:15:16 | | 5 | 瑞兹 | 25 | 900.00 | 0 | 2023-06-28 16:16:11 | +------+--------+------+------------+--------+---------------------+ 3 rows in set (0.00 sec)
mysql> select name,age, salary from person; +--------+------+------------+ | name | age | salary | +--------+------+------------+ | 盖伦 | 78 | 2637363.45 | | 艾希 | 25 | 900.00 | | 瑞兹 | 25 | 900.00 | +--------+------+------------+
二、有条件的数据查询
将where 关键加入到select语句中,可以进行有条件的数据查询
1、条件查询
select 列名称 from 表名称 where 列 运算符 值
例1:select * from person where id = 4;
例2:select * from person where id != 4;
例3:select * from person where age < 74;
例4:select * from person where age between 78 and 89;
例5:select * from person where age >=78 and age <= 89;
例6:select * from person where age >=78 or age <= 89;
2、模糊查询
使用关键字like和%、_进行模糊查询,%和_是占位符,%表示任意位字符,_表示1位字符。
例1:select * from person where name like "%袁%"; 查询name例中的字符串中有“袁”这个字符的行的数据
例2:select * from person where name like "袁%"; 查询name例中的字符串中以“袁”这个字符开头的行的数据
例3:select * from person where name like "%袁"; 查询name例中的字符串中以“袁”这个字符结尾的行的数据
例4:select * from person where name like "_袁%"; 查询name例中的字符串中第二个字符是“袁”的行的数据
例5:select * from person where name like "袁_"; 查询name例中的字符串中有两位,且以“袁”这个字符开头的行的数据
3、排序
order by关键字用于根据指定的列对结果集进行排序。 asc为默认,按升序排序;desc为降序。
例1:select * from person order by salary; #按照薪资进行排序 默认的是升序排
例2:select * from person order by salary asc; # asc 的话是默认升序的 是可以省略的
例3:select * from person order by salary desc; #按照薪资来排序,按照降序来排
例4:select * from person where age < 40 order by salary desc; #找出来年龄小于40岁的数据,然后降序来排
例5:select * from person order by salary asc, age desc; #先按照薪资排升序排,如果薪资一样再按照年龄降序排
例6、mysql> select * from person where age < 40 order by salary asc, age desc; #先按照薪资排序,如果薪资一样再按照年龄降序排,找出来年龄小于40岁的
#注:order by 只能跟在where的后面,不能放在where的前面,因为order by是对查询出的结果集进行排序,所以只能先根据条件查询出结果集,后进行排序。
4、limit关键字 限制输出
语法:limit n 查询数据输出n条
limit m,n 从第m+1条开始,输出n条
例:
mysql> select * from person; +------+--------+------+------------+--------+---------------------+ | id | name | age | salary | gender | regTime | +------+--------+------+------------+--------+---------------------+ | 3 | 盖伦 | 78 | 2637363.45 | 0 | 2023-06-28 15:31:31 | | 4 | 艾希 | 25 | 90.00 | 0 | 2023-06-28 16:15:16 | | 5 | 瑞兹 | 22 | 900.00 | 0 | 2023-06-28 16:16:11 | | 6 | 潘森 | 56 | 8292.00 | 1 | 2023-06-28 17:01:13 | | 7 | 婕拉 | 29 | 289229.00 | 1 | 2023-06-29 09:36:16 | | 8 | 亚索 | 98 | 27783.00 | 1 | 2023-06-29 09:37:48 | | 9 | 提莫 | 29 | 900.00 | 0 | 2023-06-29 09:47:02 | | 10 | 努努 | 25 | 900.00 | 1 | 2023-06-29 09:47:57 | +------+--------+------+------------+--------+---------------------+ 8 rows in set (0.00 sec) #前三条 mysql> select * from person limit 3; +------+--------+------+------------+--------+---------------------+ | id | name | age | salary | gender | regTime | +------+--------+------+------------+--------+---------------------+ | 3 | 盖伦 | 78 | 2637363.45 | 0 | 2023-06-28 15:31:31 | | 4 | 艾希 | 25 | 90.00 | 0 | 2023-06-28 16:15:16 | | 5 | 瑞兹 | 22 | 900.00 | 0 | 2023-06-28 16:16:11 | +------+--------+------+------------+--------+---------------------+ mysql> select * from person limit 0,3; +------+--------+------+------------+--------+---------------------+ | id | name | age | salary | gender | regTime | +------+--------+------+------------+--------+---------------------+ | 3 | 盖伦 | 78 | 2637363.45 | 0 | 2023-06-28 15:31:31 | | 4 | 艾希 | 25 | 90.00 | 0 | 2023-06-28 16:15:16 | | 5 | 瑞兹 | 22 | 900.00 | 0 | 2023-06-28 16:16:11 | +------+--------+------+------------+--------+---------------------+ 3 rows in set (0.00 sec) mysql> select * from person limit 3,3; +------+--------+------+-----------+--------+---------------------+ | id | name | age | salary | gender | regTime | +------+--------+------+-----------+--------+---------------------+ | 6 | 潘森 | 56 | 8292.00 | 1 | 2023-06-28 17:01:13 | | 7 | 婕拉 | 29 | 289229.00 | 1 | 2023-06-29 09:36:16 | | 8 | 亚索 | 98 | 27783.00 | 1 | 2023-06-29 09:37:48 | +------+--------+------+-----------+--------+---------------------+ 3 rows in set (0.00 sec) mysql> select * from person limit 6,3; +------+--------+------+--------+--------+---------------------+ | id | name | age | salary | gender | regTime | +------+--------+------+--------+--------+---------------------+ | 9 | 提莫 | 29 | 900.00 | 0 | 2023-06-29 09:47:02 | | 10 | 努努 | 25 | 900.00 | 1 | 2023-06-29 09:47:57 | +------+--------+------+--------+--------+---------------------+ 2 rows in set (0.00 sec)
5、SQL的内建函数
语法:select sql函数 from person;
例1:select max(age) from person; #获取年龄这一列的最大值
例2:select min(age) from person; #获取年龄这一列最小值
例3:select avg(salary) from person; #求薪资这一类数据的平均值
例4:select sum(salary) from person; #求薪资这一列数据的和
例5:select count(*) from person; #用来统计数据库里面有多少条数据的 count(*)
6、嵌套SQL
将一个SQL语句结果当成另外一个SQL语句条件来使用,开发有这种写法,但因效率太低,很少使用。
例:查询年龄最大的英雄的全部信息
select max(age) from person; #结果为98 select * from person where age = 98;
可嵌套写为:select * from person where age = (select max(age) from person);
7、分组统计
group by语句用于结合合计函数,根据一个或多个列对结果集进行分组。
例:
#按照性别进行分组统计 #select和from中间是查询的字段,如果用了group by 只能写 #当前分组的字段或者是SQL内置函数得到的结果 mysql> select gender from person group by gender; +--------+ | gender | +--------+ | 0 | | 1 | +--------+ #分组以后进行统计,统计出来 0 有几个数据 1 有几个数据 mysql> select gender, count(*) from person group by gender; +--------+----------+ | gender | count(*) | +--------+----------+ | 0 | 5 | | 1 | 4 | +--------+----------+ #按照性别进行分组,分组以后统计个数大于4的性别数 #注意事项:分组以后的条件语句用不能用where,要使用having关键字 #WHERE 关键字无法与合计函数一起使用。 mysql> select gender,count(*) from person group by gender having count(*) > 4; +--------+----------+ | gender | count(*) | +--------+----------+ | 0 | 5 | +--------+----------+ #找出来年龄小于30岁的信息,然后按照性别分组统计,统计出来的结果的个数大于2的数据 #先找年龄小于30岁的 mysql> select gender, count(*) #查什么 -> from person #从哪查 -> where age < 30 #查询条件是啥 -> group by gender #分组 -> having count(*) > 2; #分组之后的条件 +--------+----------+ | gender | count(*) | +--------+----------+ | 0 | 3 | +--------+----------+ 1 row in set (0.00 sec))
8、去重
关键字:distinct
语法:select distinct 列名称 from 表名称
例:select name from student; #结果:张三 李四 张三 赵二 王五 李四
select distinct from student #结果:张三 李四 赵二 王五