条件查询
简单条件查询
select [列参] from [表名] where [条件]; #回显满足条件的列参数
比较关系
= 等于
!=/<> 不等于
>/< 大于/小于
>=/<= 大于等于/小于等于
select
[列参]
from
[表名]
where
[条件];
范围闭区间且包括两端的值
and sal>=1000 and sal<=1500;
between...and sal between 1000 and 1500;
or job='SALESMAN' or job='MANAGER';
# 当and 与 or同时出现时 and优先级比or高 or应用括号将其括出
回显出 工资大于2500 且编号为10或20的人员信息
select ename,sal from emp where sal>2500 and (empno=10 or empno=20);
is null/is not null comm is null/comm is not null;(在MySQL当中null不能使用'='进行衡量)
in/not in job in ('SALESMAN' ,'MANAGER');/sal not in (200,500,900);
模糊查询
select [列参] from [表名] where [列参] like [字符];
在模糊查询内'%_' 都为特殊字符 前者为任意多个字符 后者为任意单个字符
select ename from emp where ename like '%o%'; #查询名字内含'o'的人员
select ename from emp where ename like '%\_%'; #查询名字内含'_'的人员(转义字符)
select ename from emp where ename like 'A%'; #查询名字首字为'A'的人员
select ename from emp where ename like '_A%'; #查询名字第二位为'A'的人员
select ename from emp where ename like '__A%'; #查询名字第三位为'A'的人员
排序
单列参数排序
select [列参] from [表名] order by [列参] [null,asc,desc];
select empno,ename,sal from emp order by sal; #默认 为所选列参升序
select empno,ename,sal from emp order by sal asc; #为所选列参 指定升序
select empno,ename,sal from emp order by sal desc; #为所选列参 指定降序
多列参数排序
select [列参] from [表名] order by [列参1] [null,asc,desc],[列参2][null,asc,desc];
(列参1起主导 若列参1值相等时 则进行列参2值的排序)
select ename,sal from emp order by sal,ename; #默认 主导及次导皆为升序
select ename,sal from emp order by sal asc,ename desc; #主导为指定升序 次导为指定降序
表处理
改变处理
alter table [表名] rename [表名]; #修改表名
alter table [表名] change [旧列参] [新列参] [原类型]; #修改表内指定列参字段名
alter table [表名] change [旧列参] [新列参] [原类型]; #同时修改表内指定列参字段名及类型
更改处理
alter table [表名] modify [列参] [新类型]; #更改表内指定列参类型
alter table [表名] modify [列参] [类型] first; #表内指定列参更改至首位
alter table [表名] modify [列参1] [类型] after [列参2]; #表内指定列参更改至某列参后位
增删处理
alter table [表名] add [列参1] [类型],[列参2] [类型]; #向表内添增新列参字段
alter table [表名] drop [列参]; #删除表内指定列参字段
向表内列参字段增添数据
insert into [表名]
values
([列参1],[列参2],[列参3],[列参4],[列参5],[列参6]),
([列参1],[列参2],[列参3],[列参4],[列参5],[列参6]);
删除表内列参整条记录
delete from [表名] where [条件列参];
修改指定列参字段数据
update [表名] set [旧列参]=[新列参] where [条件列参];