ITPub博客

首页 > 数据库 > Oracle > 条件和排序

条件和排序

原创 Oracle 作者:brj880719 时间:2017-09-21 12:23:10 0 删除 编辑

一、符号补充

用于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 语句整理

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

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

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

  18. --时间(between .. and ..):1998 年 2 月 入职的员工 名字和工资
  19. select last_name , salary
  20. from employees
  21. where hire_date between '01-2月-98' and '28-2月-98' ;

  22. --10----60 号部门员工
  23. --between .. and ..:
  24. select last_name , salary
  25.   from employees
  26.  where department_id between 10 and 60 ;
  27. --比较运算符:
  28. select last_name , salary
  29.   from employees
  30.  where department_id >= 10
  31.    and department_id <= 60;
  32.    
  33. --in:10 , 30, 70 号部门员工
  34. select last_name , salary
  35. from employees
  36. where department_id in ( 10,30,70 ) ;

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

  43. --last_name 中 倒数第三个字符是 s
  44. select last_name , salary
  45. from employees
  46. where last_name like '%s__' ;

  47. --1998 年入职的员工 名字和工资
  48. --方法一:比较查询
  49. select last_name , salary
  50.   from employees
  51.  where hire_date between '01-1月-98' and '31-12月-98' ;
  52. --方法二:通配符方式
  53. select last_name , salary
  54. from employees
  55. where hire_date like '%98';

  56. --2 月 入职的员工 名字和工资
  57. select last_name , hire_date
  58. from employees
  59. where hire_date like '%-2月%';

  60. --转译符,转译_
  61. select * from t1
  62. where a like 's_%' ;

  63. select * from t1
  64. where a like 's\_%' escape '\\';

  65. --转译*
  66. select * from t1
  67. where a like 's*_%' escape '*';

  68. --null值处理:哪些员工 没有部门号
  69. select last_name , salary
  70. from employees
  71. where department_id is null ;

  72. --哪些员工 有部门号
  73. --方法一
  74. select last_name , salary
  75. from employees
  76. where department_id > 0 ;
  77. --方法二
  78. select last_name , salary
  79. from employees
  80. where department_id is not null
  81. order by 2 ;

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

  87. --排序order by
  88. select last_name , hire_date
  89. from employees
  90. order by 2 ;

  91. --verify:使用VERIFY 命令来显示的替代变量之前和之后SQL开发人员替换替换变量的值
  92. --&:用来提示用户输入一个数值:
  93. set verify on
  94. select last_name from employees
  95. where employee_id=&1;

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/24851054/viewspace-2145260/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-12-06

  • 博文量
    75
  • 访问量
    67556