库管理命令
show databases; //查看所有库
select database(); //显示当前库
use 库名 //切换库
create database 库名 default charset utf8mb4; //创建库
drop database 库名; //删除库
show tables; //查看表
select user(); //显示连接用户
select version; //查看版本
表管理命令
create table 库名.表名 (
字段1 类型(宽度),
字段2 类型(宽度),
.......
)default charset=utf8; //指定中文字符集,可以给字段赋值中文
desc 库名.表名; //查看表结构
drop table 库名.表名; //删除表
基础查询
select 字段 from 表名; //查单个字段
select 字段1,字段2 from 表名; //查多个字段
select * from 表名; //查所有字段
select 字段1+字段2 from 表名; //使用表达式
select count(*) from 表名; //统计共有多少行记录
进阶查询
select distince 字段 from 表名; //去重
select concat(字段1,'--',字段2) from 表名; //字段拼接
select 字段 from 表名 where 条件; //条件查询
常用 > < = >= <= !=
逻辑运算符 and、or、not
select * from 表名 where 字段1=xxx and 字段2>yyy;
模糊查询
like:包含
between xxx and yyy:在xxx和yyy之间
in:在列表中
is null:为空
is not null:非空
select name,email from 表名 where name like '张%'; //%匹配0到多个任意字符
select * from 表名 where id between 1 and 5; //查询id在1到5之间的行
select * from 表名 where id in (1,3,5,8); //查询id为1,3,5,8的行
排序
select 字段 from 表名 order by 字段; //按字段升序排列
select 字段 from 表名 order by 字段 desc; //降序排列
select 字段1,字段2,字段3+字段4 as 别名 from 表名 where 字段=xxx order by 别名; //创建别名并按别名升序排列