ORACLE数据库的常用SQL语句总结(二)

17.条件控制关键字(一)

案例分析: a>b 并且ab 或者 a>c

OR:或者,当2个以上条件时,or表示满足“or”两边任意一个条件,即条件成立。

And:和/并且,但遇到2个以上条件时,and表示必须同时满足“and”两边条件,才算成立。

例如:请列出员工工号“7788”和“7521”的员工信息
Select * from ZQ_001 where enpno=7788 or enpno=7521;
Select * from ZQ_001 where enpno in(7788,7521);

例如:请列出员工“SMITH”“ALLEN”“WARD”的员工信息
select * from ZQ_001 where ename=‘SMITH’ or ename=‘ALLEN’ or ename=‘WARD’;
select * from ZQ_001 where ename=‘smith’ or ename=‘ALLEN’ or ename=‘WARD’;——”smith”小写错误(进可查询2条信息:“ALLEN”“WARD”)
select * from ZQ_001 where ename in(‘SMITH’, ‘ALLEN’ ,‘WARD’);

备注:①数据信息查询中要注意大小写;②条件语句中字符(字母、汉字)要加“单引号”,数字看不加。

18.条件控制关键字(二)

In:在…里,当判断的字段是同一个,且拥有多个数据值时使用

Between and :在…与…之间 ,当判断某字段的数据在某2个数据值之间,同时包含边界值时使用。

例如:–列出10号部门里薪水高于1000且低于3000的员工信息

select * from ZQ_001 where depotno=10 and salary>1000 and salary<3000;

select * from ZQ_001 where depotno=10 and not salary<=1000 and not salary>=3000;

select * from ZQ_001 where depotno=10 and salary between 1000 and 3000 and
not salary in(1000,3000);

select * from ZQ_001 where depotno=10 and salary between 1000 and 3000 and
not salary=1000 and not salary=3000;

19 . and 和 or :and 优先级高于 or
例如:where (A and B) or C 和 where C or A and B 意思是一样的

例如:select * from ZQ_001 where depotno=20 and salary>2000 or depotno=10;——请列出20号部门薪水大于2000的员工信息和10号部门员工所有信息

例如:请列出20,30号部门里薪水在1000余3000之间的员工信息(5种以上方式)
select * from ZQ_001 where depotno in(20,30) and
salary>1000 and salary<3000;–1

select * from ZQ_001 where depotno in(20,30) and
salary between 1000 and 3000 and not salary in(1000,3000);–2

select * from ZQ_001 where depotno in(20,30) and
not salary<=1000 and not salary>=3000;–3

select * from ZQ_001 where depotno in(20,30) and
not (salary<=1000 or salary>=3000);–4

select * from ZQ_001 where depotno in(20,30) and
salary between 1000 and 3000 and not salary=1000 and not salary=3000;–5

select * from ZQ_001 where depotno in(20,30) and
salary between 1000 and 3000 and not (salary=1000 or salary=3000);–6

select * from ZQ_001 where depotno=20 and salary>1000 and salary<3000
or depotno=30 and salary>1000 and salary<3000;–7

19.Is 与 null(空值):非空数据判断必须使用运算符“=”,而空值的判断职能用“is”
update ZQ_001 set bonus=null where bonus=0;——请将员工表中所有奖金为“0”的人的奖金设置为“null”
update ZQ_001 set bonus=0 where bonus is null;-——请将员工表中所有奖金为“null”的人的奖金设置为“0”

20.字符函数:解决字符串链接问题
① Concat(charA,charB) 次括号中只允许有2个参数
②“||”连接符,表示“+”
如,要求显示“某某的月薪是XX元,月奖金是XX元,职位是XX”
Select concat(concat(concat(concat(concat(concat(ename,‘的月薪是’),salary),‘元,月奖金是’),nvl(bonus,0)),‘元,职位是’),job) from ZQ_001;

select ename ||‘的月薪是’|| salary || ‘元,月奖金是’ || nvl(bonus,0) || ‘元,职位是’ || job from ZQ_001;

21.取反查询 not :写在判断字段之前(判断空值null是可以将“not”写在“is”后面)
如,select * from ZQ_001 where not job=‘MANAGER’;——请列出员工表中职位不是“MANAGER”的员工信息

22.Upper(char)/ lower(char)/ initcap(char)函数 :转换英文字母大小写
Upper:将字母转换为大写形式;lower:将字母转换为小写形式;intcap:将字符串中每个单词首字母转换为大写,其余为小写,之间用空格或字母表示。
如,select upper(‘hello world’) from dual;——HELLO WORLD
select initcap(‘hello world’) from dual;——Hello World
select initcap(‘HELLO WORLD’) from dual;——Hello World
如,select * from ZQ_001 where ename=‘SMITH’; select * from ZQ_001 where lower(ename)=‘smith’; —-请列出员工“SMITH”的所有信息

23.模糊匹配查询:like(像、类似)
“ _”:表示占有一个位置的字符,有且仅有一个;”%”:表示占有多个位置的字符(0~无穷)

select * from ZQ_001 where ename like '_A%' and ename like '%E_'; 或 select * from ZQ_001 where ename like  '_A%E_' or ename like ‘EA’;

——列出员工表中姓名第二个字母为大写A,且倒数第二个位大写E

24.排序查询:order by(永远写在末尾) 格式: select * from 表名 order by 排序的字段名(Oracle 数据库中,null默认最大值)

如,select ename,salary from ZQ_001 order by salary; ——排序查询:对员工薪水进行排序,显示员工姓名和薪水(升序)
如,select ename,bonus from ZQ_001 order by bonus;——-对员工奖金排序,显示姓名和奖金
如,select ename,hiredate from ZQ_001 order by hiredate;——对员工入职日期排序,显示姓名和入职日期
如,select ename,bonus from ZQ_001 where not bonus=0 and bonus is not null
order by bonus;——请列出员工表中没有奖金的员工姓名和奖金,按姓名排序显示

25.降序查询:desc 格式:select * from 表名 order by 字段名 desc;
如,select ename,salary from ZQ_001 order by salary desc,ename desc;——对员工表中的薪水进行降序排序,若薪水相同,则按姓名降序排序,显示员工姓名和薪水

注意:①ASC与DESC:排序默认为升序排列(系统默认设置值为ASC),ASC表示升序,DESC表示降序;②当多个字段为排序依据时,首先按第一个字段排序,然后再按照第二个字段排序,以此类推,每个字段都需单独设置排序方式。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值