mysql 命令行客户端--命名集

# 登录  
mysql -uroot -p
# 显示当前时间
select now();
#登出
quit 或 exit 或 ctrl + d

#查看所有数据库
show databases;

#创建数据库
create databases 数据库名 charset = utf8;

#使用数据库
use 数据库名;

#查看当前使用的数据库
select database();

#删除数据库
drop database 数据库

show tables

create table user ( id,int unsinged primary key auto_increment not null,
	name varchar(33),
	age varchar(33)
	 )

#修改	 
alter table students add birthday datetime;

#删除表
drop table 表名

select * from table
select id,name from students;

insert into students values(0, 'xx', default, default, '男');

delete from 表名 where 条件

update students set age = 18, gender = '女' where id = 6;

-- 表名.字段名
select students.id,students.name,students.gender from students;

-- 可以通过 as 给表起别名 
select s.id,s.name,s.gender from students as s;

select * from students where id = 1;
等于: =
大于: >
大于等于: >=
小于: <
小于等于: <=
不等于: !=<>

and
or
not

like是模糊查询关键字
%表示任意多个任意字符
_表示一个任意字符

between .. and .. 表示在一个连续的范围内查询
in 表示在一个非连续的范围内查询

判断为空使用: is null
判断非空使用: is not null


排序

asc从小到大排列,即升序
desc从大到小排序,即降序
select * from students where gender=1 and is_delete=0 order by id desc;

select * from 表名 limit start,count
limit是分页查询关键字
start表示开始行索引,默认是0
count表示查询条数

select * from students where gender=1 limit 0,3;
简写
select * from students where gender=1 limit 3;


# 聚合函数
聚合函数又叫组函数


count(col): 表示求指定列的总行数
max(col): 表示求指定列的最大值
min(col): 表示求指定列的最小值
sum(col): 表示求指定列的和
avg(col): 表示求指定列的平均值

-- 返回非NULL数据的总行数.
select count(height) from students; 

-- 返回总行数,包含null值记录;
select count(*) from students;

-- 查询女生的编号最大值
select max(id) from students where gender = 2;

-- 查询未删除的学生最小编号
select min(id) from students where is_delete = 0;

-- 查询男生的总身高
select sum(height) from students where gender = 1;

-- 平均身高
select sum(height) / count(*) from students where gender = 1;

-- 求男生的平均身高, 聚合函数不统计null值,平均身高有误
select avg(height) from students where gender = 1;

-- 求男生的平均身高, 包含身高是null的
select avg(ifnull(height,0)) from students where gender = 1;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值