1.运算符
1.1 算数运算符
# 返回的结果为结果集 resultset
select 1+1 sum;
select 2-1;
select 5/2; # 2.5
select 5 div 2; #取整
select 5/0; # null
select 5%2; #取余
1.2 比较运算符
# 比较运算符
select 1>2; # 0
select 2>1; # 1
select 1!=2;
select 1<>2; # 不等于
1.3 逻辑运算符
#逻辑运算符
select 1>2 and 2>1; #与
select 1>2 or 2>1; #或
select !(1>2); # 非
1.4 位运算符
# 位运算符
select 2&3; # 10 2
select 2|3; # 11 3
select 2^3; # 01 1
2. DML操作
2.1 添加/插入
insert into tname [(col1,col2...)] values(val1,val2...);
2.2 修改
update tname set col1=val1,col2=val2...[where 条件];
如果需要唯一确定某条记录,最好使用主键作为条件
2.3 删除
delete from tname [where 条件];
drop truncate delete 区别?
drop: 删除整个表结构
truncate: 表结构未删除,清空所有数据,重置了自增的结构
delete: 删除数据,只与数据有关,与结构无关,不会充值自增
3. DQL操作
select 字段/表达式(控制结果集的内容)
from 表/视图/结果集(控制从什么地方查询)
where 条件(简单,组合条件等)
order by 字段(排序) asc:升序 desc: 降序
group by 字段(分组)
having 条件(分组之后再次过滤)
limit(分页,限制结果查询 只针对mysql)
-
查询语法
#1.查询emp表中所有记录信息 select * from emp; #2.查询emp表中所有的员工名称和薪水 select ename,sal from emp; #3.在emp表中查询员工编号为7788的员工的姓名和所在部门编号 select ename,deptno from emp where empno=7788; #4.在emp表中查询20号部门超过2000的员工信息 select * from emp where deptno=20 and sal >2000; #5.在emp表中查询员工编号为7369,7521,7788的员工信息 select * from emp where empno=7369 or empno=7521 or empno=7788; #6.在emp表中查询工资在1000到2000之间的员工信息(范围查询) select * from emp where sal between 1000 and 2000; #7.在emp表中查询员工编号为7369,7521,7788的员工信息(集合查询) select * from emp where empno in (7369,7521,7788); #8.查询emp表中所有的职位信息(去重操作 distinct) select distinct job from emp; #9.查询员工工资提升15%之后的员工姓名和薪水(别名) select ename,sal*1.15 sal from emp; #10.在emp表中查询没有奖金的员工信息(空的判断用is null) select * from emp where comm is null; #11.按照工资的升序查询员工信息(order by 字段 asc|desc) select * from emp order by sal asc,empno desc; #12.查询ename以s开头的所有员工信息(模糊查询 like) %:0到多个字符 select * from emp where ename like 's%'; select * from emp where ename like '%n'; select * from emp where ename like '%l%'; #第二个字符是l _:代表一个字符 select * from emp where ename like '_l%'; #13.查询前五条记录(限制结果查询 limit index,length) select * from emp limit 5; select * from emp limit 0,5;
###4. 函数
4.1 单行函数
-
数学函数
select abs(-16); #绝对值 select floor(12.5); #向下取整 select ceil(12.3); #向上取整 select round(12.5); #四舍五入 select PI(); select mod(5,2); #取模运算 select POW(2,3); #次方 select SQRT(9); #开方 select rand(); # 随机数 [0,1)
-
字符函数
select length('this is an apple'); select length(ename) from emp; select LOWER('THIS IS'); #大写转换小写 select UPPER('this is'); #小写转换大写 select SUBSTR('abcde',1,2);#截取字符串长度 select REPLACE('abcde','bc','ggg');# 替换 select trim(' abc '); #去空格 select LPAD('abc',10,'*');#从左侧填充字符到指定长度 select RPAD('abc',10,'*');#从右侧填充字符到指定长度
-
日期函数
select SYSDATE(); #获取系统时间 select NOW(); #现在 select CURRENT_DATE(); select CURRENT_TIME(); select CURRENT_TIMESTAMP(); select YEAR('1998-1-5'); select MONTH('1998-1-5'); select DAY('1998-1-5'); #实现日期的运算 select DATE_ADD('1998-10-15',interval 3 day); #获取指定时间该月的最后一天 select LAST_DAY('2020-11-26');
4.2 聚合函数
select max(sal) from emp;# 最大值
select min(sal) from emp;# 最小值
select avg(sal) from emp;# 平均数
select count(*) from emp;# 统计记录数
select count(comm) from emp; #统计该字段不为空的数目
select sum(sal) from emp; # 求和
####4.3 分组函数
group by 字段
# 获取每个职位的平均工资
select job,avg(sal) from emp group by job;
使用分组函数, select子句中能够出现聚合函数和分组的字段
having:分完组之后再次实现检索
1.where 子句在group by之前 having在group by 之后
2.where 子句中不能使用聚合函数 having 中可以使用聚合函数
####4.4 加密函数
# 不可逆的加密 只能加密 不能解密
select MD5('root');
select sha('root');
select password('root');