成都10班-潘伟 发表于 2021-11-22 20:04:19

Day11_笔记

单表查询
1.查询表内所有数据
   select * from 表名;
select * from student; 查询student表中所有数据
2.查询某个字段的数据
   select 字段名 from 表名;
select name from student; 查询student表中name例中的数据
3.查询多个字段的数据
   select 字段1,字段2,.. from student;
   select name,class,.. from student;查询表中
4.查询满足某个条件的所有数据
select * from 表名 where 条件;
select * from student where id=3;
5.查询满足某个条件的所有数据
例:select * from studen where class!=1833;
    查询class不是1833的所有数据
!=或 <>   不等于
6.查询同时满足多个条件的所有数据
select * from student where 条件1 and 条件2;
例:select * from student where class=1833 andmath>80;
查询class=1833且math>80的所有数据
7.
8.查询一个条件范围内的数据
select * from 表名 where 字段betweenn and m;
例:select * from student where age between 25 and 31;
       select * from student where age>=25 and age<=31;
查询年龄在25-31的所有数据
9.查询某一个条件为null的数据
select * from student where 字段名 is null;
例:select *from student where class is null;
      查询class为null的所有数据
10.查询字段不满足在指定集合中的数据
   select * from 表名 where 字段 not in(值1,值2,值3,..)
例:select * from student where class not in(1833,1835,1825);
11.查询字段值为空的数据
select * from 表名 where 字段 is null;
注意:字段是空不能写成 字段=null
12.查询字段不为空的数据
    select * from 表名 where 字段 is not null;
13.查询某个字段模糊匹配成功的数据
    select * from 表名 where 字段 like ‘%值%’;%用于匹配字段开头和结尾
例: select * from student wherename like 'xiao%';
14.查询限定字段开头和结尾
   select * from 表名 where 字段 limit m,n;(m指下标,n指限定的记录数量)
15.查询的数据根据某个字段从小到大排序
select * from 表名 order by 字段 asc;
16.查询的数据根据某个字段从大到小排序
select * from 表名 order by 字段 desc;
17.查询的数据根据某个字段进行分组
select * from 表名 group by 字段;
18.查询的数据根据某个字段进行分组再条件过滤
select * from 表名 group by 字段 having 条件
单表查询总结
1.where不能纺放在GROUP BY后面
where 子句的作用是在对查询结果进行分组前,将不符合where条件的行去掉,即在分组之前过滤数据,条件中不能包含聚组函数比如SUM(),AVG()等,使用where条件显示特定的行。
2.having 是跟GROUP BY连在一起用的,放在GROUP BY 后面,此时的作用相当于WHERE。
having 子句的作用是筛选满足条件的组,即在分组之后过滤数据,条件中经常包含聚组函数,使用having 条件显示特定的组,也可以使用多个分组标准进行分组。

mysql聚合函数
19.统计查询数据的数量
select count(*) from 表名;
例:select count(*) from student;
20.查询某个字段求和
select sum(字段) from 表名;
21.查询某个字段求均值
select avg(字段) from 表名;
22.查询某个字段最大值
select max(字段) from 表名;
23.查询某个字段最小值
select min(字段) from 表名;
24.对某个字段进行去重查询
select distinct(字段) from 表名;

mysql 增删改语句
1.表中插入数据
insert into 表名 value(字段1的value,字段2value,字段3value,..);
2.一次性表中插入多条数据
insert into 表名 value(字段1的value,字段2value,字段3value,..),(字段1的value,字段2value,字段3value,..),..;
3.对表中指定字段插入数据
insert into 表名(字段1,字段2)values(字段1的value,字段2value);
4.删除表中指定数据
delete from 表名 where 条件;
delete from student where id=3;
5.清空表数据
truncate 表名;
6.删除表
drop table 表名;


注∶TRUNCATE,,DELETE,DROP放在一起比较∶
TRUNCATE TABLE∶删除内容、释放空间但不删除定义。DELETE TABLE∶删除内容不删除定义,,不释放空间。
DROP TABLE∶删除内容和定义,释放空间。
7.更新表中指定字段数据
update +表名set 字段名=值 where 条件
备份表,备份数据,备份数据库,还原数据库
1.备份表,创建一个表与某个表相同
create table 表名1 like 表名2;
2.备份数据,把一个表的数据插入到另一个表
insert into 表名1 select * from 表名2;
注意:插入的表必须是存在的
3.把一个表的某些字段插入到一个新表中
insert into 表名1(字段1,字段2)select 字段1,字段2 from 表名2;
insert into student1(id,name)select id,name from student;
注意:1.插入的表必须存在 2.插入的表是新表,没有数据
4.备份数据库
mysqldump -uroot -p 数据库名>/../脚本名;
5.还原数据库
mysql -uroot -p 数据库名(空的)

页: [1]
查看完整版本: Day11_笔记