条件和排序

简介: 一、符号补充 用于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条件一起运用。

一、符号补充

用于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
  • 使用DEFINE定义了的变量,可以使用&引用声明的变量。其作用范围或生命周期通常是整个会话。
  • 如果定义了变量后,需要清除变量,则可以使用UNDEFINE清除变量
  • 使用DEFINE VARIABLE来查看变量

四、where 语句整理

--数字比较(<):工资小于6000
select last_name ,salary
from employees
where salary < 6000 ;

--字符串比较(=):员工 名字,查询King 的工资
select last_name ,salary
from employees
where last_name='King' ;

--时间比较(=)
--雇佣日期是 1998.07.01 的员工 名字, 工资
--确定时间格式
select sysdate from dual;
22-oct-11
--查询名字和工资
select last_name , salary
from employees
where hire_date = '01-jul-98' ;

--时间(between .. and ..):1998 年 2 月 入职的员工 名字和工资
select last_name , salary
from employees
where 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 , salary
from employees
where department_id in ( 10,30,70 ) ;

--模糊查询(_单个字符):
--模糊查询(%多个字符,长度不固定)
--last_name 中 第三个字符是 s
select last_name , salary
from employees
where last_name like '__s%' ;

--last_name 中 倒数第三个字符是 s
select last_name , salary
from employees
where 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 , salary
from employees
where hire_date like '%98'; 

--2 月 入职的员工 名字和工资
select last_name , hire_date
from employees
where 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 , salary
from employees
where department_id is null ;

--哪些员工 有部门号
--方法一
select last_name , salary
from employees
where department_id > 0 ;
--方法二
select last_name , salary
from employees
where department_id is not null 
order by 2 ;

--and:
--名字 S 开头,并且工资高于 8000 的员工
select last_name , salary
from employees
where last_name like 'S%' and salary > 8000 ;

--排序order by
select last_name , hire_date
from employees
order by 2 ;

--verify:使用VERIFY 命令来显示的替代变量之前和之后SQL开发人员替换替换变量的值
--&:用来提示用户输入一个数值:
set verify on
select last_name from employees
where employee_id=&1; 
目录
相关文章
|
3月前
|
Java 测试技术
统计满足条件的子集个数
统计满足条件的子集个数
35 0
|
8月前
哇,三目(条件)的顺序真有趣
@(太开心) 今天看到这样的代码
27 1
|
3月前
有关筛选条件的问题
有关筛选条件的问题
29 0
|
10月前
|
存储 算法 C++
计算1到n的和(不用循环且逐步限制条件)
注:满足题目要求的解法有递归实现的第三种、公式实现、C++调用构造函数累加法三种方法、
36 0
sort()排序以及多个属性数组对象排序(按条件排序)
sort()排序以及多个属性数组对象排序(按条件排序)
84 0
|
SQL 数据库
对查询结果进行排序
对查询结果进行排序
77 0
118.求满足特异条件的数列
118.求满足特异条件的数列
98 0
|
存储 索引 Go
对聚集表查询的时候,未显式指定排序列的时候,默认查询结果的顺序一定是按照聚集索引顺序排序的吗
原文:对聚集表查询的时候,未显式指定排序列的时候,默认查询结果的顺序一定是按照聚集索引顺序排序的吗 本文之外可参考另外一篇文章作为补充:http://www.cnblogs.com/wy123/p/6189100.
892 0