oracle篇

查询工资在10000-20000之间
--表示10000<salary<20000:
select * from employees where salary>10000 and
salary < 20000

--表示 10000<=salary<=20000:
select * from employees where salary between 10000 
and 20000
查询工资是17000,8000的都有谁
select * from employees where salary = 17000 or salary = 8000
--或者
select * from employees where salary in (17000,8000) 

批量操作时候选择in, in可以短路,使得查询效率高一些

模糊查询 like

与通配符配合
通配符常用的有2个:
_:任意一个字符 --可以使用多个
%:任意字符串 – 可以有0个,也可以多个

--员工名字含有a的:
select * from employees where first_name like '%a%' 

如果我们要把通配符写在字符串里,则需要用套一字 escape

select * from employees where first_name like '%#%%' escape '#'
--输出中就包含%
运算符的优先级

1.算术运算符
2.连接符
3.比较运算符
4.is null, like, in
5.between
6.not
7.and
8.or

实际开发过程中,不要把表达式写的过于复杂。很多运算时,用括号

单引号

字符串用单引号
如果想把单引号写进字符串里,用'''hello world!''',这样输出的就是'hello world!'

排序子句

在实际开发中尽量不在数据库中使用排序,如果要使用,可在前端排。
select 字段列表 from 表名 where 条件 order by 字段 asc/desc

--按照工资的降序排序
select * from employees order by salary desc  

--工资大于10000的升序排序
select * from employees where salary > 10000 order by salary asc 

--局部的二次排序,先按照salary升序,再按照employee_id降序
select * from employees where salary > 10000 order by salary asc,employee_id desc 
oracle 中的伪列

伪列的查询结果为自增的编号

最常用的是rownum

--rownum 是根据查询结果进行自增编号
select  rownum, employee_id ,first_name from employees where salary > 10000 

--查询前5条记录:
select  rownum, employee_id, first_name from employees where rownum < 6


字符函数

1.lower
将字符串中大写字母全部变成小写

select  lower( first_name),employee_id from employees 

2.upper
将字符串中小写字母全部变成大写

select  upper( first_name),employee_id from employees 

3.initcap
将字符串中首字母大写(只要不是字母和数字进行拼接)

select  upper( first_name),employee_id from employees 

4.concat
将2列连接成一列

select  concat( first_name,employee_id)from employees 

5.substr
截取字符串

--从第二个开始截取到最后
select  substr( 'abcde',2from dual
输出:bcde
--从第二个开始截取1个
select  substr( 'abcde',21from dual
输出:b

6.instr
返回下标

select  instr( 'abcde','de'from dual

--利用下标进行模糊查询:

--查询员工姓名中含有A/a的
select * from employees where instr(lower(first_name), 'a')>0 

--查询员工姓名中首字母是A/a的
select * from employees where instr(lower(first_name), 'a')=1
数学函数

1.round
四舍五入

select round(a,b) :将a四舍五入,保留b位小数
select round(3.46,1) from dual
--输出:3.5
select round(3.46) from dual
--输出:3

2.trunc
截断

select trunc(a,b) -- :将a截断,保留b位小数
select trunc(3.461) from dual
--输出:3.4
select trunc(3.46) from dual
--输出:3

3.mod
取余数

select mod(a,b) --:a/b 的余数
select mod(2,5) from dual
--输出:2
select mod(5,2) from dual
--输出:1

4.ceil
返回>=本身的最小整数

select mod(-3.1) from dual
--输出:-3

5.floor
返回<=本身的最大整数

select floor(-3.1) from dual
--输出:-4
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值