Oracle2

来源:互联网 发布:杭州恩牛网络 上市 编辑:程序博客网 时间:2024/06/05 04:41

接我自己写的Oracle1
想在这里吐槽一句 也是告诫看到该本章的学弟学妹们 我曾经在大学时候也学过Oracle课程但是在学校没有认真听过课 直到有一天用到了才后悔莫及 于是付出更多去恶补 “还债”的滋味真不好受

4 条件查询 where

语法:select 列明1 , 列明2 from 表名 where 过滤条件 order by 列 asc|desc

4.1 等值 =

查询工资是17000 的员工号 收入select employee_id , salary from employeeswhere salary = 17000查询姓 King 的员工select employee_id , last_name from employeeswhere last_name = 'King'

注意:字符串比较中 字符串的内弄严格区分大小写。

4.2 关系 > >= < <= and or

工资超过10000的员工select employee_id , salary from employeeswhere salary > 10000;工资在10000到20000之间的员工select enployee_id , salary from employeeswhere salary > 10000 and salary < 20000;

and 表示与 or 表示或

4.3 NULL 的处理 :列名 is [not] NULL

查询没有奖金的员工idselect employee_id , commission_pct from employees where commission is NULL;

注意 is 是为空 is not是不为空
commission = NULL ; 是错误的写法

4.4 范围查询:列名 [not] between 小值 and 大值

查工资在10000-20000之间的员工idselect employee_id , salary from employeeswhere salary between 10000 and 20000;

注意:小值在前面 大值在后面 该分为是一个闭区间

4.5 枚举查询: 列名 [not] in (值1,值2,值3,…,值N)

查在60 ,70 ,90 部门的员工方案一select employee_id ,department_id from employees where department_id = 60 or department_id = 70 or department_id = 90;方案二(枚举)select employee_id ,department_id from employeeswhere department_id in (60,70,90);

注意:in 的效率较低 ,开发时不建议使用。

4.6 模糊查询:列名[not] like ‘格式字符串’

4.6.1 模糊查询

!格式字符串:包括字符串常量|通配符
其中
—“ % ”—代表0-N个随意的字符;
—“ _ ”代表占位符 表示此处只有一个随意的字符

查 K 开头的员工selectemployee_id, last_name, salary from employees where last_name like 'K%';查工资的第二位数是3的员工selectemployee_id, last_name, salary from employees where salary like '_3%';查姓的长度是8第3个字母是k的员工selectemployee_id, last_name, salary from employees where last_name like '__k_____';查询98年入职的员工selectemployee_id,last_name,salary from employees where hire_date like '%98'; 

4.6.2 模糊查询中的转义字符 escape

语法:列名 like ‘格式字符串’ escape ‘转义字符’。
在查询时,如果碰到查询条件里还有特殊字符时,可以是哟个转义字符解决问题,但是Oracle中没有固定的转义字符,如果需要使用,需要通过escape关键字声明。

查询名字是以'S_'开头的员工信息select employee_id , first_name , salary from employeeswhere first_name like 'S\_%' escape '\'

注意:like后的内容必须在 ”(单引号)中,like 是模糊查询,所以”中必须有通配符。
模糊查询时,Oracle会将列值转换为字符串后进行比较。

4.7 case … when 语法结构

基本语法:case… when 语句类似与Java|C语言中的switch case语句,用于根据不同条件返回不同的值
其语法结构为:

case    when exp1 then 结果1   when exp2 then 结果2 …else 其他结果end

其中 exp1等 均为布尔表达式。
执行流程是如果exp1为真,则返回value1 ,同是忽略后面的exp2等判断 。
如exp1为假 , 则接下来判断exp2。以此往后推。
如所有的expN都为假 , 则返回else后面的其他结果。
注意:else并不是必须出现在when结构中 若exp全假切无else时 返回null

了解了基本语法后我们运用到SQL语句中select  first_name ,salary,case when salary < 6000 then 'low'when salary between 6000 and 10000 then 'middle'else 'high'endas 工资等级from employees

其运行结果为:
这里写图片描述

注意 then语句中的返回值可以是字符串 ,日期,数字。但一定不能是布尔值
所有返回值的数据必须一致

如果我的汇总对你有帮助请点赞关注 如果你对我的代码有疑惑或者意见欢迎提出,本人邮箱:lfw615@foxmail.com

原创粉丝点击