条件和排序

来源:互联网 发布:windows访问samba 编辑:程序博客网 时间:2024/05/15 16:42

一、符号补充

用于where比较条件的有: 

1、等于: =、<、<=、>、>=、<> 

2、包含: in、not in、 exists、not exists 

3、范围: between……and、not between……and 

4、匹配测试: like、not like 

5、Null测试: is null、is not null 

6、布尔链接: and、or、not

通配符:
在where子句中,通配符可与like条件一起运用。在Oracle中: 

1、%(百分号):  用来表示任意数量的字符,或者可能根本没有字符。 

2、_(下划线):  表示确切的未知字符。 

3、?(问号):  用来表示确切的未知字符。 

4、#(井号):  用来表示确切的阿拉伯数字,0到9. 

5、[a-d](方括号): 用来表示字符范围,在这里是从a到d.

二、模糊查询

我们可以在where子句中使用like关键字来达到Oracle模糊查询的效果;

在Where子句中,可以对datetime、char、varchar字段类型的列用Like关键字配合通配符来实现模糊查询

三、变量

如果不使用替换变量,每次操作我都都要修改脚本。非常不便,如果使用替换变量,我们可以将带变量的语句存放在sql脚本中,每次运行时,只需要输入替换变量的值就可以了。

1、&:&引用的替换变量只在当前SQL有效

2、&&:&&引用的替换变量则在当前会话有效

3、SET VERIFY:如果要显示SQL*Plus使用替换值替换后的脚本文件,可以使用SET VERIFY ON/OFF 命令

4、SET DEFINE:在存储过程或包体里面,经常有在字符串中使用&的情况,执行脚本时,经常会将这些字符串视为替换变量,要求输入值,这样烦不甚烦,其实只需要设置一下SQL*PLUS的环境变量即可避免这种情况。通常通过SET DEFINE OFF

5、DEFINE

  a) 使用DEFINE定义了的变量,可以使用&引用声明的变量。其作用范围或生命周期通常是整个会话。

  b) 如果定义了变量后,需要清除变量,则可以使用UNDEFINE清除变量

  c) 使用DEFINE VARIABLE来查看变量

四、where 语句整理

--数字比较(<):工资小于6000select last_name ,salaryfrom employeeswhere salary < 6000 ;--字符串比较(=):员工 名字,查询King 的工资select last_name ,salaryfrom employeeswhere last_name='King' ;--时间比较(=)--雇佣日期是 1998.07.01 的员工 名字, 工资--确定时间格式select sysdate from dual;22-oct-11--查询名字和工资select last_name , salaryfrom employeeswhere hire_date = '01-jul-98' ;--时间(between .. and ..):1998 年 2 月 入职的员工 名字和工资select last_name , salaryfrom employeeswhere hire_date between '01-2月-98' and '28-2月-98' ;--10----60 号部门员工--between .. and ..: select last_name , salary  from employees where department_id between 10 and 60 ;--比较运算符:select last_name , salary  from employees where department_id >= 10   and department_id <= 60;   --in:10 , 30, 70 号部门员工 select last_name , salaryfrom employeeswhere department_id in ( 10,30,70 ) ;--模糊查询(_单个字符):--模糊查询(%多个字符,长度不固定)--last_name 中 第三个字符是 sselect last_name , salaryfrom employeeswhere last_name like '__s%' ;--last_name 中 倒数第三个字符是 sselect last_name , salaryfrom employeeswhere last_name like '%s__' ;--1998 年入职的员工 名字和工资--方法一:比较查询select last_name , salary  from employees where hire_date between '01-1月-98' and '31-12月-98' ;--方法二:通配符方式select last_name , salaryfrom employeeswhere hire_date like '%98'; --2 月 入职的员工 名字和工资select last_name , hire_datefrom employeeswhere hire_date like '%-2月%'; --转译符,转译_select * from t1 where a like 's_%' ;select * from t1 where a like 's\_%' escape '\\';--转译*select * from t1 where a like 's*_%' escape '*';--null值处理:哪些员工 没有部门号select last_name , salaryfrom employeeswhere department_id is null ;--哪些员工 有部门号--方法一select last_name , salaryfrom employeeswhere department_id > 0 ;--方法二select last_name , salaryfrom employeeswhere department_id is not null order by 2 ;--and:--名字 S 开头,并且工资高于 8000 的员工select last_name , salaryfrom employeeswhere last_name like 'S%' and salary > 8000 ;--排序order byselect last_name , hire_datefrom employeesorder by 2 ;--verify:使用VERIFY 命令来显示的替代变量之前和之后SQL开发人员替换替换变量的值--&:用来提示用户输入一个数值:set verify onselect last_name from employeeswhere employee_id=&1; 


原创粉丝点击