Chinaunix首页 | 论坛 | 博客
  • 博客访问: 123668
  • 博文数量: 37
  • 博客积分: 2094
  • 博客等级: 大尉
  • 技术积分: 380
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-14 08:39
文章分类
文章存档

2010年(37)

分类: Oracle

2010-04-06 21:17:11

条件和排序
 
条件:where子句后实现
排序:order by子句后实现
 
在这之前先来查询EMPLOYEES表
SQL> select employee_id,last_name,job_id department_id from employees;
如果我们需要选择出部门100的员工,如何做呢?
我们可以用where子句限制返回的行,where子句跟着from子句。格式:
select * |{[distinct] column | expression [alias] ,...} from table [where econdition(s)];
如:SQL> select employee_id,last_name,job_id,department_id from employees where department_id=100;
条件中使用字符串和日期
在WHERE子句中字符串和日期必须包含在单引号(‘’)中。但是,数字常数不应该包含在单引号中。且所有的字符搜索是大小写敏感的。   
oracle数据库以内部数字格式存储日期:世纪、年、月、日、小时、分和秒。默认的日期显示是DD-MON-YYYY。
几种常见的比较条件
oracle SQL中的比较条件包含以下几种运算符号:
=     等于
>     大于
>=    大于等于
<     小于
<=    小于等于
<>    不等于
!=    不等于
^=    不等于
between...and...    在两个值之间(包含)
in(set)   匹配一个任意值列表
like   匹配一个字符模板
is null   是一个空值
is not null   不是一个空值
 
使用比较条件:
SQL> select last_name,salary from employees where salary<=2300;
 
使用between条件:
SQL> select last_name,salary from employees where salary between 3000 and 5000;
between...and...实际上是由oracle服务器转变为一对and条件:(a>=下限)and(a<=上限),因此between...and...并没有性能上的提高,只是逻辑上的简单
 
使用in条件
SQL> select employee_id,last_name,salary,manager_id from employees where manager_id in (100,101,201);
在in条件中可以使用任何数据类型。如:
SQL> select employee_id,manager_id,department_id from employees where last_name in('hartstein','vargas');
如果in条件中的成员是字符或日期,它们必须放在单引号(‘’)中。in条件同样也是没有得到性能上的提高,只是逻辑上简单了。
 
使用like条件:
有些时候不知道要搜索的确切的值,这个时候可以用like条件匹配一个字符模板的行。oracle世界中有两个通配符(%和_)可以用来构造搜索串。其中%表示0个或多个字符,_表示一个字符。如:
SQL> select first_name from employees where first_name like 's%';
SQL> select last_name from employees where last_name like '_o%';
我们也可以使用escape标识符可以搜索实际的%和_符号,比如你想要搜索包含‘SA_’的字符串,可以使用如下命令:
SQL> select employee_id,last_name,job_id from employees where job_id like '%SA\_%AN' escape '\';
而如果你想要搜索包含%SA的字符串,可以使用下面的命令:
SQL> select * from test where department_name like '\%SA%'  escape '\';
escape选项指定反斜线(\)为换码符,在SQL命令中,换码符字符在下划线和百分号的前面,原因是oracle服务器逐字解释字符串。
 
使用NULL/NOT NULL条件
SQL> select last_name,manager_id from employees where manager_id is null;
SQL> select last_name,job_id,commission_pct from employees where commission_pct is not null and rownum<10;
 
逻辑条件
逻辑条件用于组合两个比较条件的结果来产生一个基于这些条件的单个结果,或者逆转一个单个条件的结果。SQL有三个逻辑运算符,分别是:and or not 如:
SQL> select employee_id,last_name,job_id,salary from employees where salary>=10000 and job_id like '%MAN%';
SQL> select employee_id,last_name,job_id,salary from employees where salary>=10000 or job_id like '%MAN%';
SQL> select last_name,job_id from employees where job_id not in ('IT_PROG','ST_CLERK','SA_REP');
SQL> select last_name,job_id from employees where job_id not like '%A%';
 
同样not运算符也可以用于另一个SQL运算符,如:in between null
.......where job_id not in ('AC_ACCOUNT','AD_VP')
.......where salary not between 10000 and 15000
.......where commission_pct is not null
 
优先规则
优先规则定义表达式求值和计算的顺序,默认情况下SQL的执行顺序为:算术运算、连字操作、比较操作、is[not]null,like,[not]in5、[not]between、not逻辑条件、and逻辑条件、or逻辑条件。如:
SQL> select last_name,job_id,salary from employees where job_id='SA_REP' or job_id='AD_PRES' and salary>15000;
第一个条件是:job_id是AD_PRES并且薪水高于15000
第二个条件是:job_id是SA_REP
 
使用括号强制优先权:
SQL> select last_name,job_id,salary from employees where (job_id='SA_REP' or job_id='AD_PRES') and salary>15000;
 
排序
排序分为升序、降序
语法格式:
select expr from table [where condition(s)] [order by {column,expr} [asc|desc]];
order by指定排序显示返回的行,asc以升序排列,desc以降序排列。order by子句在select语句的最后(除非使用了for update语句)。如:
SQL> select last_name,job_id,department_id,hire_date from employees order by hire_date;
 
降序排列
SQL> select last_name,job_id,department_id,hire_date from employees order by hire_date desc;
默认情况下oracle系统是将查询的结果进行升序排列的
 
反转默认排序
SQL> select last_name,salary from employees order by 2 desc;
 
用列别名进行排序
SQL> select employee_id,last_name,salary * 12 annsal from employees order by annsal;
 
多列排序
SQL> select last_name,department_id,salary from employees order by department_id,salary desc;
 
 
阅读(1993) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~