今日分享===数据库

今天分享一点数据库的知识例子.
–条件查询: select *|字段名字… from 表名 where 行过滤条件;
–执行顺序: from–where–select
–查询30部门的员工
select * from emp where deptno=30;

– 查询员工名字为’SMITH’的员工信息
select * from emp where ename=‘SMITH’;

–比较条件 = 、>、 <、 >=、 <=、 !=、 <>
–除了’SMITH’的所有员工的员工姓名和员工编号
select empno,ename from emp where ename!=‘SMITH’;
select empno,ename from emp where ename<>‘SMITH’;
select empno,ename from emp where not ename=‘SMITH’;

–查询呢薪资大于800的员工信息
select * from emp where sal>800;

–and且、 or或、 not非
–查询工种为CLERK的,并且是30部门的员工信息
select * from emp where job=‘CLERK’ and deptno=30;
–查询工种为CLERK的,或者是30部门的员工信息
select * from emp where job=‘CLERK’ or deptno=30;
–查询不是工种为CLERK的,也不是30部门的员工信息
select * from emp where job!=‘CLERK’ and deptno!=30;
select * from emp where not job=‘CLERK’ and not deptno=30;
–select * from emp where not (job=‘CLERK’ and deptno=30);
select * from emp where not (job=‘CLERK’ or deptno=30);

–查询有奖金的人
select * from emp where comm is null;
–查询没有奖金的人
select * from emp where not comm is null;
select * from emp where comm is not null;
–select * from emp where comm not is null;

– 查询工资在1500~2500之间的员工工种和姓名和工资
–查询的内容:ename,sal,job
–来源:emp
–条件:sal>1500 and sal<2500
select ename,sal,job from emp where sal>=1500 and sal<=2500;
select ename,sal,job from emp where sal between 1500 and 2500; --范围区间 从小到大

–子查询
– 查询 销售部(SALES) 中 工资大于1500的员工信息
–查询销售部的部门编号
select deptno from dept where dname=‘SALES’;
select * from emp where sal>1500 and deptno =30;
select * from emp where sal>1500 and deptno =(select deptno from dept where dname=‘SALES’);

– 查询工资比SMITH高的同一部门的员工信息
–查询的内容:*
–来源:emp
–条件:sal>SMITH的工资 and deptno=SMITH的部门编号
select sal from emp where ename=‘SMITH’; --SMITH的薪资
select deptno from emp where ename=‘SMITH’; --SMITH的部门编号
select *
from emp
where sal > (select sal from emp where ename = ‘SMITH’)
and deptno = (select deptno from emp where ename = ‘SMITH’);

select empno, ename, deptno, sal
from (select *
from emp
where sal > (select sal from emp where ename = ‘SMITH’)
and deptno = (select deptno from emp where ename = ‘SMITH’));

–数据来源 也可以是一个结果集 ,可以是表…

–查询工资大于1500 或 含有佣金的人员姓名
select ename from emp where sal>1500 or comm is not null;
select ename from emp where sal>1500; --薪资大于1500的员工姓名
select ename from emp where comm is not null; --有佣金的人员姓名

–对结果集求和|并集 相同的数据保留一个 Union
select ename from emp where sal>1500
Union
select ename from emp where comm is not null;

–Union All,全集(不去重)
select ename from emp where sal>1500
Union All
select ename from emp where comm is not null;

–查询显示不存在雇员的所有部门号
–查询所有部门编号
select deptno from dept;
–查询有员工存在的部门编号
select distinct deptno from emp;
–差集 Minus,差集( 减去重复 )
select deptno from dept
Minus
select distinct deptno from emp;

–查询工资大于1500 且 含有佣金的人员姓名
select ename from emp where sal>1500 and comm is not null;
–Intersect,交集(找出重复)
select ename from emp where sal>1500
Intersect
select ename from emp where comm is not null;

–模糊查询|匹配 like %任意个任意字符 _一个任意字符
–查询姓名以’A’开头的员工信息
select * from emp where ename like ‘A%’;
– 查询姓名里边有‘A’的员工信息
select * from emp where ename like ‘%A%’;
– 查询名称中第二个字母为‘A’的员工信息
select * from emp where ename like ‘_A%’;
– 查询名称以’H’结尾的员工信息
select * from emp where ename like ‘%H’;

insert into emp(empno,ename,sal) values(1000,‘t_%test’,8989);
insert into emp(empno,ename,sal) values(1200,‘t_tes%t’,8000);
–查询姓名中含有%的员工信息
select * from emp where ename like ‘%A%%’ escape(‘A’); --A为转义字符,转义A后面的字符

–事务 在对一个表中的数据做增加,删除,修改的时候,会自动开启事务
commit;
rollback;

– 查询工资为 1500, 2000, 2500, 5000的员工的信息
select * from emp where sal=1500 or sal=2000 or sal=2500 or sal=5000;
–多个定值之间满足任意一个就可以
select * from emp where sal in (1500,2000,2500,5000);
–部门名称为 SALES 或 ACCOUNTING 的雇员信息
–数据: *
–来源:emp
–条件:员工的部门名称为 SALES 或 ACCOUNTING
–查询SALES 或 ACCOUNTING的部门编号
select deptno from dept where dname in (‘SALES’,‘ACCOUNTING’);
–查询10,30部门的员工信息
select * from emp where deptno in(10,30);

select *
from emp
where deptno in
(select deptno from dept where dname in (‘SALES’, ‘ACCOUNTING’));
– 查询工资等级为 2的员工信息
–数据: *
–来源:emp
–条件:sal>2等级的最低薪资 and sal<2等级的最高薪资
–2等级的最低薪资
select losal from salgrade where grade=2;
select hisal from salgrade where grade=2;
select *
from emp
where sal >= (select losal from salgrade where grade = 2)
and sal <= (select hisal from salgrade where grade = 2);

select *
from emp
where sal between (select losal from salgrade where grade = 2) and
(select hisal from salgrade where grade = 2);

–exists 存在即保留存在即合法
select *
from emp
where exists (select deptno from dept where dname = ‘SALES’);

–查询’SALES’, 'ACCOUNTING’部门的员工信息
select *
from emp
where exists (select deptno
from dept
where dname in (‘SALES’, ‘ACCOUNTING’)
and emp.deptno = dept.deptno);

–查询除了’SALES’, 'ACCOUNTING’部门的员工信息

select *–所有数据都能保留
from emp
where exists (select deptno
from dept
where dname in (‘SALES’, ‘ACCOUNTING’)
and emp.deptno != dept.deptno);

select *
from emp
where not exists (select deptno
from dept
where dname in (‘SALES’, ‘ACCOUNTING’)
and emp.deptno = dept.deptno);

–表别名
select *
from emp e
where not exists (select deptno
from dept d
where dname in (‘SALES’, ‘ACCOUNTING’)
and e.deptno = d.deptno);

delete from emp where empno in(1200,1000);

–所有有奖金的员工信息
select empno, ename, sal,comm
from emp e1
where exists (select empno, ename, sal, comm
from emp e2
where comm is not null
and e1.empno = e2.empno);
–有奖金的员工所在的部门中的所有员工的信息
select empno, ename, sal, comm ,deptno
from emp e1
where exists (select empno, ename, sal, comm,deptno
from emp e2
where comm is not null
and e1.deptno = e2.deptno);

–获取所有的航纪录
select * from emp;
select * from emp where 1=1; --java中常使用,比较灵活不需要判断,也不影响结果
select * from emp where ename like ‘%’;

select * from emp where ename like ‘SMITH’;

–排序 order by 字段 desc(降序)|asc(升序),字段 排序方式 默认升序 nulls last(所有的null值在最后显示)|fist
select * from emp order by empno desc;

–查询30部门的所有员工,根据薪资升序排序 默认升序
select * from emp where deptno=30 order by sal desc;

select * from emp where deptno=30 order by comm nulls last;

—查询10,30部门的所有员工,根据薪资降序排序,如果薪资相同根据员工编号降序排序
select * from emp where deptno=30 or deptno=10 order by sal desc,empno desc;

–select *|字段1… from 表|结果集 where 行记录条件 order by 排序字段 排序方式,…;
–执行顺序: from–where–select–order by

函数

– 当前时间
select sysdate from dual;
select current_date from dual;

– 2天以后是日期
select sysdate+2 from dual;

– 所有员工入职的3天后是几号
select empno,ename,hiredate,hiredate+3 from emp;

– 查询所有员工的试用期期到期(转正的日期) 3个月试用期
select ename 员工姓名, hiredate 员工入职日期, hiredate+30*3 员工转正日期 from emp;
–月份相加减
select ename 员工姓名, hiredate 员工入职日期, add_months(hiredate,-3) 员工转正日期 from emp;

– 查询所有员工到目前为止一共工作了几个月 months_between(大日期,小日期}
select months_between(sysdate,hiredate) from emp;

– 查询当前月的最后一天
select last_day(sysdate) from dual;

– 下一个星期三是几号
select next_day(sysdate,‘星期二’) from dual;

–字符串转为日期对象 to_date(‘字符串’,‘日期对象的模板’)
– 设定一个特定的时间(用一个特定的时间字符串转换为日期对象)
– ‘2019/6/18 14:20:13’
select to_date(‘2019/6/18 14:20:13’,‘yyyy/mm/dd hh24:mi:ss’)+2 from dual;

–日期对象转为字符串 to_char(日期对象,‘字符串模板’)
– 将日期转为特定格式的字符串
select to_char(sysdate,‘yyyy"年"mm"月"dd"日" hh12:mi:ss’) from dual;

–给每个部门后后面添加一个伪列,如果10部门,伪列显示为十,二十,三十…
–decode(判断字段,值1,结果1,值2,结果2…,默认值) 判断函数
select deptno,decode(deptno,10,‘十’,20,‘二十’,30,‘三十’,40,‘四十’) 中文名 from dept;

– 给20部门的所有员工都涨薪10%,显示出员工的名称, 原来的薪水, 所属部门编号, 涨薪后的薪水
select ename,sal “原薪水”,deptno, decode(deptno,20,sal*1.1,sal) “涨薪后的薪水” from emp;
–case when then else end
select ename,
sal “原薪水”,
deptno,
(case deptno
when 20 then
sal * 1.1
else
sal
end) “涨薪后的薪水”
from emp;

– 10部门涨薪10%, 20涨薪20%,30降薪1% , 40部门翻倍3倍

– 查询82年入职员工的信息
select *
from emp
where hiredate between to_date(‘1982-01-01’, ‘yyyy-mm-dd’) and
to_date(‘1982-12-31’, ‘yyyy-mm-dd’);

select *
from emp
where hiredate >= to_date(‘1982-01-01’, ‘yyyy-mm-dd’)
and hiredate <= to_date(‘1982-12-31’, ‘yyyy-mm-dd’);

select *
from emp
where to_char(hiredate,‘yyyy’) like ‘1982%’;

聚合函数

–多行函数|组函数|组合函数|聚合函数 max() min() avg() sum() count(字段|1|*)

– 统计一下一共有多少个员工
select count(empno) from emp;
select count(1) from emp;
select count(deptno) from emp;

– 统计一共有几个部门
select count(deptno) from dept;

– 统计有员工存在的部门总数
select count(distinct deptno) from emp;
select count(distinct 1) from emp;

– 统计20部门一共有多少人
select count(*) from emp where deptno=20;

– 计算本公司每个月一共要在工资上花费多少钱
select sum(sal) from emp;

– 计算20部门每个月的工资花销
select sum(sal) from emp where deptno=20;

– 查询本公司的最高工资和最低工资 max() min()
select max(sal),min(sal) from emp;

–查看30部门的最高工资和最低工资,平均工资 avg()
select max(sal),min(sal),avg(sal),count(sal) from emp where deptno=30;
select * from emp;

–注意:
–**组函数只能和组函数和分组字段一起使用
–**组函数不能在where中使用
– 请查询出 20部门的平均工资, 部门编号
select avg(sal) from emp where deptno=20;

– 计算出所有员工的奖金总和
select sum(comm) from emp;

– 统计有奖金的员工有几个
select count(comm) from emp;
select count(1) from emp where comm is not null;

–查询 最高薪水的员工姓名, 及薪水
–查询的数据: ename,sal
–数据的来源: emp
–条件: sal=最高薪水
–最高薪水
select max(sal) from emp;
select ename,sal from emp where sal=(select max(sal) from emp);

– 查询工资低于平均工资的员工编号,姓名及工资
select empno,ename,sal from emp where sal< (select avg(sal) from emp);

猜你喜欢

转载自blog.csdn.net/weixin_45116848/article/details/92957111