源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Oracle基础学习之简单查询和限定查询

  • 时间:2020-02-26 15:09 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Oracle基础学习之简单查询和限定查询
本文主要介绍的是关于Oracle中的简单查询和限定查询,下面话不多说,来一起看看吧。 [b]SQL:[/b]      1,DML(数据操作语言):主要指的是数据库的查询与更新的操作,查询操作是整个sql语法 中最麻烦也是笔试中最常用的部分。      2,DDL(数据定义语言):主要指的是数据对象的创建(表、用户、)例如:creat.需要相关的设计范式。      3,DCL(数据控制语言):主要进行权限的操作(需要结合用户来观察),此部分由DBA负责。 [b]简单查询:[/b] 1,利用[code]select [/code]子句控制要显示的数据列:
 select empno,ename,ename,job,sal from emp;
2,可以使用[code]distinct[/code]来消除重复的数据行显示:
 select distinct job from emp;
3,[code]select[/code]子句可以进行四则运算,可以直接输出常量内容,但是对于字符串使用单引号数字直接编写,日期格式按照字符格式:
select empno,ename,(sal*15+(200+100)) income from emp;
4,||负责输出内容连接此类的操作很少直接在查询中出现:
 select empno||ename from emp;
5,[code]where[/code]子句一般都写在[code]from[/code]子句之后,但是是紧跟着[code]from[/code]子句之后执行的,[code]where[/code]子句控制显示数据行的操作,而[code]select[/code]控制数据列,[code]select[/code]子句要落后于[code]where[/code]子句执行,所以在[code]select[/code]子句之中定义的别名无法在[code]where[/code]中使用。 [b]限定查询:[/b] [b]1,关系运算符:[/b]
 select * from emp where sal>1500;

 select * from emp where ename ='SMITH'

 select empno,ename,job from emp where job<>'SALESMAN';
[b]2,逻辑运算符:[/b]
 select * from emp where sal>1500 and sal<3000;
 select * from emp where sal>2000 or job='CLERK';
 select * from emp where not sal >=2000;
[b]3,范围查询:[/b]
 select * from emp where sal between 1500 and 2000;
 select * from emp where hiredate between '01-1月-1981'and'31-12月-1981';
[b]4,空判断[/b](空在数据库上表示不确定,如果在数据列使用null不表示0)
select * from emp where comm is not null;
[b]5,IN操作符[/b](类似于[code]between and [/code]而in给出的是指定的范围):
 select * from emp where empno in (7369,7566,7788,9999);
关于[code]not in[/code]与[code]null[/code]的问题: 在使用[code]not in [/code]进行范围判断的时候,如果范围有[code]null[/code],那么不会有任何结果返回。 [b]6,模糊查询:[/b] “-”:匹配任意一位字符; “%”:匹配任意的0,1,,或者多位字符; 查询姓名是以字母A开头的雇员信息:
 select * from emp where ename like 'A%'
查询姓名第二个字母是A的雇员信息:
select * from emp where ename like '_A%';
查询姓名任意位置是A的雇员信息:
 select * from emp where ename like '%A%';
[b]查询排序:[/b] ASC(默认):按照升序排列; DESC: 按照降序排列; 查询所有的雇员信息,要求按照工资的由高到低:
 select * from emp order by sal desc;
查询每个雇员的编号,姓名,年薪,按照年薪由低到高排序:
  select empno ,ename,sal*12 income from emp order by income;
语句的执行顺序:[code]from [/code]- [code]where [/code]-[code]select [/code]- [code]order by[/code] [b]基础练习:[/b] 1,选择部门30中的所有员工:
 select * from emp where deptno=30;
2,列出所有办事员(clerk)的姓名,编号,和部门编号:
select ename,empno,deptno from emp 
where job='CLERK';
3,找出佣金高于薪金的60%的员工:
 select * from emp where comm>sal*0.6 ;
4,找出部门10中所有的经理(manager)和部门20中所有的办事员(clerk):
 select * 
 from emp 
 where (deptno=10 and job='MANAGER' )or(deptno=20 and job='CLERK'   );
5,找出部门10中所有的经理(manager),部门20中的所有办事员(clerk),以及既不是经理又不是办事员但是工资高于等于2000的所有员工资料:
 select *
 from emp
 where (deptno=10 and job='MANAGER')or(deptno=20 and
 job='CLERK')or(job! ='MANAGER'and job!='CLERK' and sal>=2000);
  select *
  from emp
  where (deptno=10 and job='MANAGER')or(deptno=20 and job='CLERK')or(job
  not in ('CLERK','MANAGER') and sal>=2000);
6,找出收取佣金的员工的不同工作:
select distinct job
 from emp
 where comm is not null;
7,找出收取佣金或者收取的佣金低于100的员工:
  select distinct job
  from emp
 where comm is null or comm<100;
8,显示不带有“R”的员工姓名:
 select *
 from emp
 where ename not like '%R%';
9,显示姓名字段含有A的所有员工姓名,显示的结果按照基本的工资由高到低,如果工资相同,则按照雇佣年限由早到晚,如果雇佣日期相同,则按职位排序:
 select * from emp where ename like '%A%' order by sal desc,hiredate
 asc,job;
[b]总结[/b] 以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部