mysql 给min值取别名_mysql基本命令

创建数据库:

create database 数据库名字

create database user;

创建表:

create table 表名(id int(4)  age int(4) name char(20));

create table user(id int(4) age int(4) name char(20));

显示所有的数据库:

show databases;

查看数据库中所有的表单;

show tables;

显示创建表的详细信息:

show create table 表名;

show create table name;

选择数据库:

use 数据库名字;

use name;

删除数据库;

drop database 数据库名字;

drop database name;

删除表;

drop table 表名;

drop table name;

查看表结构;

desc 表名;

desc name;

添加表一个字段;

alter table 表名 add 新字段名称    字段属性;

alter table name add sex int(4);

删除表的一个字段;

alter table 表名 drop 删除字段名称;

alter table name drop id;

修改表名;

alter table 旧表名 rename 新表名 ;

alter table name rename user;

修改表一个字段;

alter table 表名 change 旧字段名称  新字段名称  字段属性;

alter table name change id id int(4);

自增长属性和主键的添加删除:一张表中只能有一个字段是自动增长的,并且被设定为自动增长的这个字段一定要设置为主键;

如果一个主键字段有自增长属性,如果想要直接删除主键,这是操作不了的;必须是先将自增长属性去除,然后才能删除主键;

alter table name change id id int(4) auto_increment;  #添加自增长属性;#必须先添加主键才能添加自增长属性,不能直接添加

alter table name change id id int(4);#删除自增长属性;

alter table name change id id int(4) primary key;#添加主键

alter table name drop primary key;删除主键 #添加了自增长属性的,必须先删除自增长属性才能删除主键,不能直接删除

增加数据:

insert into 表名(字段1,字段2,字段3......)  values(值1,值2,值3.....);

insert into name(id,age,sex)  values(1005,20,"男");

insert into name values(1005,20,"男");#必须是个表单结构是对应的;一次写入多个数据;

insert into 表名(字段1,字段2..) values(值1.1,值2.1...),(值1.2,值2.2,....);

insert  into  name(id,name,sex,age)values('1001','张三','男',20),('1002','李四','男',21);

#插入的是日期,和字符串一样,使用引号括起来。更新表单数据:如果where子句没有指定,则默认将表中的数据全部更新

update 表名 set  字段=值  where  条件;

update name set sex="男" where id=1001;  修改id=1001的学生性别

update name set setmath=98,chinese=93 where  id=1002;修改id=1002的数学和语文成绩

update name set chinese=chinese+5;给所有学生的语文成绩+5;

update name set chinese=chinses+5 where id>1003;给id大于1003的学生语文成绩加5;删除表单数据;如果where子句没有指定,则默认将表中的数据全部删除

delete from 表名 where 条件;

delete from name where id=1003;  删除id=1003的学生信息

delete from name;  删除表中的全部数据

查询数据;select * from 表名; #查询表中的所有数据

select * from name;select * from 表名 where 条件;#查询表中id =1004的数据

select * from nam where id=1004;select id,sex from name;#查询指定字段的数据select sex as "性别" from name;#查询指定sex字段的数据,并且给添加“性别”别名;as (用于添加别名);as不是给表里的字段取别名,而是给查询的结果字段取别名。其目的是让查询的结果展现更符合人们观看习惯,在多张表查询的时候可以直接的区别多张表的同名的字段。

条件查询;

模糊查询;

%: 表示任意个或多个字符,可匹配任意类型和长度的字符

_: 匹配任意一个字符

select * from name where name like "王%"; 查询出学生姓王的学生信息;like 后面跟查询的信息

select * from name where name like "%王%";查询出学生姓名中带王的学生信息

select * from name where name like "%王";查询出学生姓名中最后一个字是王的学生信息

and 并且查询;

select * from nam where  sex="男" and name like "%王%";查询学生为男性,并且姓名中带有王字的学生的信息;

or 或者,满足任意一个条件即可;

select * from name where birth>1990 or sex="男";查询出生日期大于1990,或者为男性的学生信息;

between x and y //在x与y之间的

select * from name where birth between 1989 and 1992;查询出生日期在1989到1992之间的学生信息;

select * from user where birth between 1989 and 1992 and name like "王%";查询出生日期在1989到1992之间的学生信息并且姓名为王的学生信息:

select * from user where birth between 1989 and 1992 and id between 905 and 907;查询出生日期在1989到1992之间的学生信息并且id 在905到907之间的学生信息;

in在..里面,允许规定多个值:

select * from  name where  class="1704"or class="1703"; # 显示1703和1704两个班级学生信息;

select * from  name where  class   in("1703","1704‘’);

limit 指定显示多少行,limit后面2个数字,用逗号隔开,第一个表示数字后。第二数字表示显示

select * from name limit 10;显示前10行数据

select * from name limit 3,4;显示第4行到底7行的数据;

select * from name limit 3,2;显示第4行到第5行的数据,limlit使用的是chinese排序

select * from name where english between 70 and 90 limit 0,2;显示english成绩在70—90的开头两行数据

select department,sex,count(sex) from user group by department,sex;

select department,sex,count(sex) from user group by department,sex having sex="女";7、逻辑运算符                                   8、算术运算符

> 大于                                                        + 加法

< 小于                                                         - 减法

>= 大于等于                                              * 乘法

<= 小于等于                                              / 除法

=  等于                                                        % 取余

!= 不等于

其他(聚合函数)

1、count() 统计数量

select class ,count(class) from user ;#统计所有班级的数量

select class,count(class) from user group by class;#统计每个班级的数量  group by 分组

select department,sex,count(sex) from user group by department,sex having sex="女";#统计每个院系女生的数量

having 表示条件(类似where) 虚拟字段是不可以放在where后面的,例如where avg(math)其中math是实际字段,但avg(math)是求出来的平均分是虚拟字段; 需要进行条件处理的虚拟字段,可以放在having后面;

2、avg() 求平均分

select avg(math) from user;#统计全部班级的数学平均分

select class,avg(math) from user group by class;#统计每个班级的数学平均分

3、sum() 求和

select sum(math)  from user;#统计全部班级的数学总分

select class,sum(math) from user group by class;#统计每个班级的数学平均分:

4、max()最大值

select max(math)  from user;#统计全年级数学坐高分

select class,max(math) from user group by class;#统计每个班级的数学最高分

5、min()最小值

select min(math)  from  user;#统计全年级的数学最少分

select class,min(math) from user group by class;#统计每个班级的数学最高分

6、distinct() 去重复

select distinct(name) from  user ;#去除名字相同的学生

7、order by 排序

select * from  user order by id;// 默认是升序

select name,sum(grade) from score group by name order by sum(grade) ;#统计每个学生的总分,成绩按照从低到高列出

select * from  user  order by id desc;// 降序

select name,sum(grade) from score group by name order by sum(grade) desc;#统计每个学生的总分,成绩按照从高到底列出

8、group by 分组

select  class,count(class)  from  user group by class;

9、having 表示条件(类似where) 虚拟字段是不可以放在where后面的,例如where avg(math)其中math是实际字段,但avg(math)是求出来的平均分是虚拟字段; 需要进行条件处理的虚拟字段,可以放在having后面;

select department,sex,count(sex) from user group by department,sex having sex="女";

show databases;#查看所有数据库

show tables;#查看数据下的所有表单

use 数据库名字;#选择数据库

creater databse 数据库名称;#查看数据库

creater table 表名(字段,int());#创建表单

drop database 数据库名字;#删除数据库

drop table 表名;删除表单;

alter table 表名 drop 字段;#删除表单的某个字段

alter table name drop primary key;#删除主键

alter table 旧表名 rename 新表名;修改表名

alter table 表名 add  字段 字段属性;#表单中添加字段

alter table 表名 change 旧字段 新字段 新字段属性;#修改某个字段

alter table 表名 change 旧字段 新字段 新字段属性

alter table 表名 change 添加主键的字段 字段 字段属性 primary key;#添加主键

alter table 表名 change 添加自增长字段 字段 字段属性 auto_auto_increment;#添加自增长属性

alter table 表名 change 删除自增长字段 字段 字段属性;#删除自增长属性

desc 表名;#查看表单结构

insert into  表名(字段1,字段2,.......) values(值1,值2,.......);#表中添加数据

insert into  表名(字段1,字段2,.......) values(值1,值2,.......),(值1,值2,.......);#一次性添加多个数据

insert into 表名 values(值1,值2,..........);#添加的数据必须与表单结构对应

update 表名 set 字段=值;#更改这个字段的所有值

update 表名 set 字段=值 where 条件;#x更改某个字段的值,条件为更改所在字段的某个字段值,如:(id=1001)或者(name="张三")等值都可以。

select * from 表名;查看表数据

avg():平均

select avg(math) from user;#统计全部班级的数学平均分

sum():求和

select sum(math)  from user;#统计全部班级的数学总分

max():求最大值

select max(math)  from user;#统计全年级数学坐高分

min():最小

select min(math)  from  user;#统计全年级的数学最少分

distinct():去重

select distinct(name) from  user ;#去除名字相同的学生

order by:排序

select * from  user order by id;// 默认是升序

select * from  user  order by id desc;// 降序

group by:分组

select  class,count(class)  from  user group by class;#group by 给班级分组

having同where

select department,sex,count(sex) from user group by department,sex having sex="女";#having和where类似一样,区别是having后面跟虚拟字段

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值