进入数据库 use 数据库名;
查看数据库、表 show databases; show tables;
查看表结构 show columns from 表名;
服务器状态 show status;
显示授权用户 show grants; ######################################################### 查看表中单个列 select 列名 from 表名;
查看表中多个列 select 列名1,列名2,列名3 from 表名;
查看表中所有列 select * from 表名;
查看表中一个列不同的行 select distinct 列名 from 表名;
查询某个表的某个列,返回5条记录 select 列名 from 表名 limit 5; 从5行开始的5行 select 列名 from 表名 limit 5,5;
使用完全限定的表名 select 表名.列名 from 表名; ########################################################## 查看某个表的某个列,以字母顺序排序 select 列名 from 表名 order by 列名;
查看某个表的多个列,先以价格再以字母排序 select 列名1,列名2,列名3 from 表名 order by 列名2,列名3;
查看某个表的某个列,指定顺序方向(降幂) select 列名1,列名2 from 表名 order by 列名2 desc; select 列名1,列名2 from 表名 order by 列名2 desc,列名3;
desc降幂 asc升幂
排序+限制 select 列名 from 表名 order by 列名 limit 1; ########################################################### 查看某个表的某个列,并使用条件进行筛选 select 列名 from 表名 where 列名 = 值;
符合:=,<>,!=,<,<=,>,>=,between
查询空值 select 列名 from 表名 where is null; ########################################################### 组合where语句 select 列名1,列名2,列名3 from 表名 where 列名1 = 值 and 列名2 <= 值;
in语句 select 列名1,列名2 from 表名 where 列名3 in (值1,值2) order by 列名1;
not语句用来否定where语句 select 列名1,列名2 from 表名 where 列名3 not in (值1,值2) order by 列名1; ########################################################### 使用%通配符,进行模糊查询 select 列名1,列名2 from 表名 where 列名1 like 'jet%'; '%jet%' 's%e' 使用_通配符,进行模糊查询 select 列名1,列名2 from 表名 like '_ ton anvil'; '% ... ...' ############################################################ 正则 select 列名 from 表名 where 列名 regexp '.000' order by 列名; '1000|2000' '[123] ton' '1|2|3 ton' '[1-5] ton' '.' '\\.' 查找点.的 [:alnum:]任意字母和数字 [:alpha:]任意字母 [:digit:]任意数字 [:upper:]任意大写字母 [:lower:]任意小写字母 ############################################################ 计算平均值 select avg(列名) as 别名 from 表名; 计算总数 select count(*) as 别名 from 表名; mix() min() sum() ############################################################ 更新删除数据 update 表名 set 列名 = '值' where 列名 = '值';
delete from 表名 where 列名 = 值;
|