MySQL学习2--数据查询
一、基础数据查询select语句用于从表中选取数据,结果被存储在一个结果表中(称为结果集)。
语法:select * from 表名称 #查询指定表中的所有数据 *为模糊匹配所有列
例:
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)或 select 列名称 from 表名称 #查询指定表中指定列的数据,仅显示匹配的列的数据,未进行匹配的不显示
例:
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 =78 or ageselect * 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(*) fromperson group by gender;
+--------+----------+
| gender | count(*) |
+--------+----------+
| 0 | 5 |
| 1 | 4 |
+--------+----------+
#按照性别进行分组,分组以后统计个数大于4的性别数
#注意事项:分组以后的条件语句用不能用where,要使用having关键字
#WHERE 关键字无法与合计函数一起使用。
mysql> select gender,count(*) from persongroup 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 #结果:张三 李四 赵二 王五
来源:https://www.cnblogs.com/y15136702274/p/17512628.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页:
[1]