MySQL命令合集
1.查询
创建数据库:create database 数据库名 charset=utf8;
使用数据库:use 数据库名;
创建数据表:create table 数据表名(字段名 类型 条件);
查询所有字段:select * from 表名;
消除重复行:在select后面字段名前面使用distinct即可
*注意:在命令最后面加上 ;
2.条件
语法:select * from 表名 where 条件;
2.1 比较运算符:
等于: =
大于: >
大于等于: >=
小于: <
小于等于: <=
不等于: != 或 <>
例:select * from students where id <= 5;
2.2 逻辑运算符:
and
or
not
例:select * from students where id > 3 and gender=0;
2.3 模糊查询:
like
%表示任意多个任意字符
_表示一个任意字符
例:select * from students where name like ‘黄%’;
2.4 范围查询:
in表示在一个非连续的范围内
between … and …表示在一个连续的范围内
2.5 空判断:
判空is null
判非空is not null
例:select * from students where height is not null;
3.排序
语法:select * from 表名 order by 列1 asc|desc;
asc 升序(默认)
desc 降序 ##聚合函数
总数:count(*)
最大值:max(字段名)
最小值:min(字段名)
求和:sum(字段名)
平均值:avg(字段名) ##分组
group by
4.分页
语法:select * from 表名 limit start,count
5.连接查询
语法:select * from 表1 inner或left或right join 表2 on 表1.列 = 表2.列;
inner join 内连接
left join 左连接
right join 右连接
6.子查询
in