MySQL的语句查询——单表查询

一、字段操作语句

1、新建表

2、建表语句

create table student( id int primary key , name char(20), sex char(10), age int(3), mobile char(20), class char(10), english int(10), chinese int(10), math int(10) )engine=innodb default charset=utf8;

insert into student values (1,'小红','女',23,'13813828824','1719',77,88,98), (2,'小明','男',23,'13713713711','1720',56,66,55), (3,'小李','男',23,'15915913911','1719',78,64,87), (4,'小张','男',23,'15915913912','1720',77,76,77), (5,'小白','女',24,'15915913913','1719',90,89,98), (6,'小陈','女',19,'15915913914','1719',84,100,81), (7,'小钱','女',20,'15915913915',null,45,99,93);

3、操作语句(表结构)

(1)add  添加字段

格式:alter  table   表名  add   字段名  字符类型(字符长度);

案例:alter  table   student  add   dcs  int(10);

(2)change 修改字段

格式:alter  table   表名  change  源字段名   新字段名  字符类型(字符长度);

案例:alter table   student  change  dcs   hzdcs  int(10);

(3)drop 删除字段

格式:alter table 表名 drop 字段名;

案例:ALTER  table    student  drop   hzdcs ;

(4)rename 修改表名

格式:ALTER  table     表名  rename  新表名 ;

案例:ALTER  table     student  rename  student2 ;

(5)modify after   字段的调换

格式:alter  table   表名  modify  源字段名  字符类型(字符长度)  after  表中字段名;

案例:alter  table   student2  modify  math int(10)  after  name ; 

(6)first  添加字段到第一位

格式:ALTER  table   表名    add  新字段名  字符类型(字符长度)  first ; 

案例:ALTER  table   student2    add  no  int(10)  first ; 


二 、查询语句

1、select 查询所有数据

格式:select * from 表名;

案例:select * from student2;

2、查询部分数据

格式:select  字段1,字段2  from  表名;

案例:select  name,math  from  student2 ;

3、查询字段可以用as取别名

格式:select  字段1 as "别名名称",字段2 " 别名名称2"  from  表名 ;

(备注:as也可以省略不写)

案例:select  name as "姓名",math " 数学"  from  student2 ;

4、where接指定条件查询内容

(1)比较运算符:>,<,=,!=,<>,>=,<=

*案例:

1)=等于:select  *  from  student2  where  id=2;

2)!=不等于:select  *  from  student2  where  id!=2;

3)<>不等于:select  *  from  student2  where  id<>2;

4)大于:select  *  from  student2  where  id>2;

5)小于:select  *  from  student2  where  id<2;

6)大于等于:select  *  from  student2  where  id>=2;

7)小于等于:select  *  from  student2  where  id<=2;

(2)and 同时满足

案例:select  *  from  student2  where  id>3 and  math>90;

(3)or 满足其中一个条件就能查询出数据

案例:select  *  from  student2  where  id>6 or  math>97;

(4)beteen ... and ... 在什么范围之间

案例:select  *  from  student2  where  id  BETWEEN  2 and 5;

(5)in 在一组数据中匹配

案例:select  *  from  student2  where  id  in (2,8,6)

(6)not in 在一组数据中匹配

案例:select  *  from  student2  where  id not in (2,8,6)

(7)is null 为空

案例:select  *  from  student2  where  class  is null;

(8)is not null 不为空

案例:select  *  from  student2  where  class  is not null;

(9)like 模糊查询

%:通配符

_:代表一个字符

1)查找8开头的数据:select  *  from  student where  english  like "8%";

2)查找包含8的数据:select  *  from  student2 where  math  like "%8%";

3)查找8结尾的数据:select  *  from  student2 where  math  like "%8";

4)查看指定字符的数值:select  *  from  student where  english  like "8___";

5、排序

(1)order by

a.升序:asc 可以省略不写

b.降序:desc

(2)二次排序

select  *  from  student2 order  by  字段1  desc ,字段2 desc;

6、limit 查询指定的数据,限制

根据索引来取值,从0开始,一个表的第一行就是哦,第二数字,行数(3行)

案例:select  *  from  student  limit 0,3

7、聚合函数

(1)max 最小值:select  max(math)  from  student

(2)min 最大值:select  min(math)  from  student

(3)sum 总数:select  sum(math)  from  student

(4)avg平均数:select  avg(math)  from  student

(5)count 统计个数:select  count(math)  from  student

(6)DISTINCT 去重:select  DISTINCT(math)  from  student

-group by 分组-

*分组后的函数,智能分组的字段,一起显示,其他字段显示就是默认取第一行*

案例1:分组求出每个班级的平均数

select  class,avg(math)  from  student group  by class 

8、having+条件

案例:分组求出每个班级的平均数小于80分

select  class,avg(math) as a from  student group  by class HAVING a<80;

9、update...set...更新数据/修改数据

案例:UPDATE  student  set  name= " xh"  where id=1

10、删除数据

(1)delete

案例1:delete from student

案例2:DELETE  from  student  where  id=1

(2)drop

案例:drop table student

(3)truncate

案例:truncate student


-快捷方式-

注释:ctrl+/

取消注释:shift+ctrl+/

单行注释:#

-备份-

(1)在数据库中备份

1、备份表结构:create table 新表 like 旧表;

2、备份表数据:

insert into 新表 select * from 旧表;

insert into 新表(字段1,字段2) selcet 字段1,字段2 from 旧表;

3、备份表结构和表数据:create table 新表 as( select * from 旧表);

(2)自Linux中备份

1、备份数据

语句:mysqldump  -u root -p hh>/home/hz56.sql

 

2、还原数据

a.新建数据库

b.还原数据库

语句:mysql -u root -p  kk</home/hz56.sql

posted @ 2025-02-27 12:59  uai  阅读(15)  评论(0)    收藏  举报