成都10班-戴绍武 发表于 2021-11-22 20:09:48

数据库指令学习

select * from student 查询student表所有字段的数据,*表示所有字段
select name from student 查询student表里所有的姓名
select name,class from student 查询student表里所有的姓名和班级
select * from student where class=1833 查询班级为1833班所有信息
select * from student where class=1833 and class=1834 报错,and两边不能是同一字段等于两个不同值
select * from student where class=1833 or class=1834 如果条件是同一字段等于多个值,只能用or连接
select * from student where class!=1833 查询班级不为1833班所有信息
select * from student where class<>1833 查询班级不为1833班所有信息
select * from student where name='zhangsan' and class=1833 查询姓名为zhangsan,班级为1833班的信息。and两边可以接两个不同字段等于具体指
select * from student where age between 25 and 31 查询年龄在25到31岁之间的信息,包含25和31
select * from student where age>=25 and age<=31 查询年龄在25到31岁之间的信息,功能和between and一样,都包含25和31
select * from student where class in(1833,1834) 查询班级在1833和1834里面的信息。功能和class =1833 or class=1834一样
select * from student where class not in(1833,1834) 查询班级不为1833和1834的信息。
select * from student where class in(1833,1834) 查询班级在1833和1834里面的信息。
select * from student where class is not null 查询班级不为空的信息
select * from student where name like 'xiao%' 查询姓名以xiao开头的信息
select * from student where name like '%ao%' 查询姓名中包含ao的信息
select * from student where id limit 5 查询前5行
select * from student where id limit 1,4 查询2到5行的数据。limit m,nm是下标,n是行数,limit后面的值如何设置,第二行行号2-1作为第一个值,第二个值就是5-下标值
select count(*) from student 统计student表数据的行数
select count(class) from student      count后面尽量不要接字段名,如果要单独去统计某个字段,也是可以接单个字段名的
select sum(math) from student 查询student表数学成绩总分
select avg(math) from student 查询student表平均数学成绩
select max(math) from student 查询student表最大数学成绩
select min(math) from student 查询student表最小数学成绩
select distinct(sex) from student 对student表里的sex字段进行去重操作
select * from student order by age asc    order by用于排序的关键字,对年龄进行从小到大排序
select * from student order by age desc 对年龄进行从大到小排序
select sum(math) from student group by class 查询每个班级的数学总分
select class,sum(math) from student group by class 查询每个班的数学总分,把class字段加在select后面,方便识别查询出来的结果(哪个总分属于哪个班的)
select class,sum(math) from student group by class having sum(math)>100 查询每个班级数学总分大于100的班级和总分数
select class,sum(math)as s from student group by class having s>100   as s为给聚合函数取别名的方式
select class,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条件显示特定的组,也可以使用多个分组标准进行分组。
mysql增删改语句
1.表中插入数据
insert into + 表名(字段1value,字段2value,字段3value...)
2.一次性表中插入多条数据
insert into + 表名(字段1value,字段2value,字段3value...),(字段1value,字段2value,字段3value...)
3.对表中指定字段插入数据
insert into +表名(字段1,字段2)values(字段1值,字段2值)
4.删除表中指定数据
delete from + 表名 where 条件
5.删除表
truncate + 表名
6.删除表
drop table + 表名
insert into test values(11,'xiaoliu',2021,66.66,'','2021-11-22')   给test表所有字段都插入数据,values前面不写字段名的时候,values后面要按照字段的顺序并且每个字段都要插入数据
delete from test where sid=1   删除sid=1的一条数据
delete from test where sid in(7,8,9,10)   删除sid为7,8,9,10的数据
truncate test 删除表中所有的数据,不删除表
drop table test 删除整张表,如果表中有数据,也会连同数据一起删除
truncate table删除内容、释放空间但不删除定义
delete table删除内容不删除定义,不释放空间
drop table   删除内容和定义,释放空间
create table student1 like student 创建一个student1表,表结构和student表一样且字段也一样
insert into student1 * from student 备份数据,往student1表里插入数据,插入的数据来源是从student表查询出来的所有数据
create table student2 like student 创建一个student2表,表结构和student表一样且字段也一样
insert into student2(id,name) select id,name from student   往student2里只插入id和name的数据,数据来源与从student表查询出来的id和name的数据
mysqldump -uroot -p123456 dcs10>/dcs10.sql在Linux操作界面进行对dcs10数据库的备份,备份之后形成一个名为dcs10.sql的脚本,并放在根目录下
create database dcs10_back   备份好数据库之后,接下来是还原数据库操作。先建一个空库
mysql -uroot -p123456 dcs10_back</dcs10.sql 在linux界面进行将dcs10.sql脚本的数据还原至dcs10_back的空库里。
update student set math=100 where id=1 把id=1的数学分数改为100

页: [1]
查看完整版本: 数据库指令学习