1.mysql 语句分类
DQL:数据库查询语言
select
DML:数据库操作语言
insert
delete
update
DDL:数据定义语言
creat
drop
alter
DCL:数据控制语言
grant
revoke
TCL:事物控制语言
commit
rollback
2.数据库常用命令
- show (show databases、show tables)
- use table_nama;
- create/delete database database_name;
- select databases();
- describe(desc) table_name;
3.数据库查询语言select
- 简单查询
select 字段名1,字段名2…… from table_name - select * from table_name
- select x as y from table_name (可省略as,别名里有下划线要用单引号把别名圈起来,有中文也是,)
- 条件查询
- select x from table_name where fun;(fun代表查询条件,包括 = ,<> ,<,<=,>,>=,between……and……,is null,is not null,and,or ,in,not in like )
- 模糊查询
- select x from table_name like fun(fun是模糊的形式,包括下划线_和百分号%,下划线代表一个字符,百分号代表任意多的字符)
4、排序
- 升序 select * from table_name order by fun asc;(fun代表字段名或者列号)
- 降序select * from table_name order by fun desc;(fun代表字段名或者列号)
- asc 和desc是排序规则,如果没写就默认是asc
以上语句的执行顺序:
第一步:from
第二步:where
第三步:select
第四步:order by(排序总是在最后执行!)
5.字符串操作函数
- upper(str):转大写
- lower(str):转小写
- lentgth(str):求字符串长度
- substr(str,x,y) :取子串x,y是str的字符索引
- concat(str1,str2):字符串拼接
- trim(str):去空格
一个例子:
将某列的字符串首字母大写(小写):
select concat(lower(substr(ename,1,1)),substr(ename,1,length(ename)-1)) as result from emp where ename=trim(’ KING’);
6.数字相关函数
- str_to_date
- data_format
- format
- round():四舍五入
- rand();生成(0,1)的随机数
7.其他部分函数
- ifnull:空处理函数
- case…when…then…when…then…else…end