蔡雅利 发表于 2021-11-22 21:00:35

mysql

二、对数据库数据的增删改查操作
select ...from 表名      查
insertinto      增
delete from    删
update表名称set   改

Field:字段 id   name   score   phone   time
Type:
类型为int和bigint整型   
char 和 varchar 存储数据的时候都需要加上单引号或者双引号比如:'小周'
float:浮点型,默认保存6位    float(20,2)保留小数点后面2位
date:日期,存储日期数据也需要加引号


26、select name from student;查询student表里所有的姓名
27、select name,class from student;查询student表里所有的姓名和班级
28、select * from student where class=1833;查询student表里班级为1833
29、select * from student where class=1833 and class=1834;两个都满足(报错,不能为同一字段)
30、select * from student where class=1833 or class=1834;任意满足(如果条件为同一字段,只能用or)
31、select * from student where class!=1833;不等于 或者用<>
32、select * from 表名 where name="zhang" and class=1833; 查询姓名为zhang,班级为1833的信息, and 两边接两个不同字段等于的具体值 (都满足)
33、select * from 表名 where age between 25 and 30;查询年龄在25到31之间的信息,包含25和30
34、select * from 表名 where age>=25 and age <=31;查询年龄在25到31的信息,功能和between and 一样
35、select * from 表名 where class in (1833,1834);查询班级在1833和1834里面的信息,功能和or一样
36、select * from 表名 where class not in (1833,1834); 不在里面的
37、select * from 表名 where class is null;查询班级为空(空是属性,不是具体的值,不能用等于)
38、select * from 表名 where name like "%ao%"; 查询包含ao的内容
39、select * from 表名 where 字段 limit 1,4;查询2到5行的数据(m是下标,n是行数)



函数
1、select count(*)from 表名;统计表数据的行数
      select count(class)from 表名;
2、select sum(math) from 查询表数学成绩总分
3、select avg(math) from 表名; 查询表平均数学成绩
4、select max(math) from 表名; 最大值
5、select min(math) from 表名; 最小值
6、select distinct(sex) from 表名;对表里的sex字段进行去重操作
7、select * from 表名 order by age asc; 从小到大排序
8、select * from 表名 order by age desc; 从大到小排序


求每个班级的数学总分
select class,sum(math)from student group by class;
每个班级数学总分大于100的班级信息
select class,sum(math) from student group by class having sum(math) >100;
给查询的字段取别名
select class,sum(math)as s from student group by class having s>100
select class,sum(math) s from student group by class having s>100
select class as"班级“,sum(math)s from student group by class having s>100

单表查询总结
1、where 不能放在group by 后面
where子句的作用是在对查询结果进行分组前,将不符合where条件的行去掉,即在分组之前过滤数据,条件中不能包含聚组函数比如sum,avg等,使用where条件显示特定的行。
2、having 是跟group by 连在一起用的,放在group by 后面,此时的作用相当于where
having 子句的作用是筛选满足条件的组,即在分组之后过滤数据,条件中经常包含聚组函数,使用having条件显示特定的组,也可以使用多个分组标准进行分组。


C:\Users\Administrator\AppData\Local\YNote\data\weixinobU7VjtA-4ZeDeVLpuwb8GaxOWQs\191e905eb54048918223282a08125260\cache_3fa2c38095e246f3..jpg


C:\Users\Administrator\AppData\Local\YNote\data\weixinobU7VjtA-4ZeDeVLpuwb8GaxOWQs\6335c2b9702d40109c57006b53099f98\cache_1ab71c72cd8e717e..jpg

C:\Users\Administrator\AppData\Local\YNote\data\weixinobU7VjtA-4ZeDeVLpuwb8GaxOWQs\52e523e3fe9848ceadad23543c2ce3b0\cache_-235ea6b096af6c8e..jpg



页: [1]
查看完整版本: mysql