oracle简单查询


A:简单条件查询
语法:
select * from 表名 where 条件
B:模糊查询
语法:
select * from 表名 where 条件名 like 名称
(后面可以叫上and(和)和or(或者),当and和or一起使用时,要用()确定优先级)
C:范围查询
语法:
select * from 表名 where 范围条件(用大于/小于/等于)and 范围条件
(可以用between 范围条件 and 范围条件 实现)
D:排序查询
a:升序
语法:
select * from 表名 order by 条件(从小到大)
b:降序
语法:
select * from 表名 order by 条件 desc(从大到小)
E:基于伪列的查询
rowid(物理地址)语法:
select rowid,t.*(给*取一个别名,或者每一列单列出来) from 表名 where rowid='条件';

rownum(前面加序号)语法:
select rownum,t.* from where rownum='条件';
F:聚合统计
a:求和
语法:
select sum(条件) from 表名 where 条件
b:求平均数
语法:
select avg(条件) from 表名 where 条件
c:求最大值
语法:
select max(条件) from 表名 where 条件
d:求最小值
语法:
select min(条件) from 表名 where 条件
e:统计个数
语法:
select count(*) from 表名 where 条件
f:分组聚合统计(select 后面一定是分组聚合的条件或者是聚合函数)
语法:
select 条件 from 表名 group by 条件
g:分组聚合统计(having(对结果进行操作))
语法:
select 条件 from 表名 group by 条件 having 条件
注意:
1:where不能放在group by 后面
2:having是跟group by 连在一起用的,放在group by后面,此时的作用相当于where.
3:where后面的条件中不能有聚合函数,比如 sum()等,而having可以.
G:连接查询
a:多表内连接查询
语法:(2张表)
select * from 表1名(可以取别名),表2名 where 条件(表1与表2中相同的条件)
语法:(3张表)
select * from 表1名(可以取别名),表2名,表3名 where 条件(表1与表2中相同的条件)and 表3中相同的条件
注意:关联的时候表和表之间一定要有关联的条件,如果没有那么就要找到有相同条件的表,进行关联.

b:多表外连接查询
语法:(左)(sq1999)
select * from 表1名(可以取别名) left join 表2名 on 条件(表1与表2中相同的条件)
语法(oracle)
select * from 表1名(可以取别名), 表2名 where 条件(+)(表1与表2中相同的条件)

语法:(右)(sq1999)
select * from 表1名(可以取别名) right join 表2名 on 条件(表1与表2中相同的条件)
语法(oracle)
select * from 表1名(可以取别名), 表2名 where 条件(+)(表1与表2中相同的条件)
注意:  在oracle语法中,左外连接在右边添加(+),右外连接在左边添加(+).

H:子查询
a:单行子查询
语法:
select * from 表名 where 条件>(select * from 表名 where 条件)
b:多行子查询
语法:
select * from 表名 where 条件 in(条件中的数据,用','隔开)
注意: 如果需求当中右不包含,不包括之类的,再条件前面加上'not'就可以.
c:from子句子查询
语法:
select * from (select * from 表名 where条件)

d:select子句子查询
语法:(必须是单行子查询)

I:分页查询
a:简单分页
语法:
select rownum,t.*(取别名)from 表名 where rownum<=行数. //显示第一页数据
select * from (select rownum,t.*(取别名)from 表名 where rownum<=行数)where rownum<=行数 and rownum>行数; //显示第二页数据
b:基于排序的分页
语法:
select * from (select * rownum r,t,* from(select*from 表名 order by 条件 desc/*先进行排序*/)t)where rownum<=行数 and rownum>行数;
/*最里面进行排序,第二层嵌套给排序后的数据排序,第三层进行分页*/; 

J:单行函数
a:字符函数(dual就是一个一行一列的伪表)

1:求字符串长度 (length)
select length('字符串') from dual

2:求字符串子串(substr)
select substr('字符串',从第几位开始,截取几位字符数)from dual;

3:字符串拼接(concat)
(将第二个参数的字符串拼接到第一个字符串内,如果需要多次拼接,那么就需要在外层进行嵌套)
select concat('字符串','字符串')from dual

第二种拼接方式,推荐使用. 可以进行多次拼接,||不是或.
select '字符串'||'字符串'||'字符串'...from dual;

b:数值函数
1:四舍五入 (round)
select round(小数,获取点后几位数)from dual;

2:数字截取 (trunc)
select trunc(小数)from dual;//将点后面全部截掉,如果需要小数,name在后面添加',需要点后几位数'就可以保留小数

3:取模 (mod)
select mod(数值,数值) from dual;

c:日期函数

1:加月(aa_months)
select add_months(sysdate,2(正数为加月,负数为减月.都是基于当前月份)) from dual;

2:当月最后一天(last_day)
select last_day(sysdate) from dual;

3:日期截取(trunc)
select trunc(sysdate) from dual;//按日截取,就是把时间截掉

select trunc(sysdate,'mm') from dual;//按月截取,把日截掉
d:转换函数

1:数字转字符串
select to_char(数字) from dual;

2:日期转字符串
select to_char(sysdate,'yyyy-mm-dd') from dual;

3:字符串转日期
select to_day('2016-03-10','yyyy-mm-dd') from dual;

4:字符串转数字
select to_number('数值')+数值 from dual;

e:其他函数

1:空值处理函数(如果第一个参数为空,返回第二个参数,不为空返回第一个参数)
select nvl(检测的值,如果为null的值);
(第二种,有3个参数,参数可以转换成字符串)
select bvl2(检测的值,转换的字符串);//后面可以跟上条件

2:条件判断(除去第一个参书,第二个参数到最后的参数,都是两两对应,如果最后一个参数没有对应值,那么第一个参数对应的就是那个值(缺值))
select decode(检测的值,后面是匹配的数值)from dual;

3:cass(如果) when(为) then(那么就是)
select name,(case  when 条件 then 条件...[如果有缺值,就用else 条件] end) from 表名.

f:行列转换
将行上的条件转换成列上的条件.

g:分析函数
1:值相同,排名相同,序号跳跃
select rank()over(order by 条件 desc ) 排名(别名),t.* from 表名

2:值相同,排名相同序号连续
select dense_rank()over(order by 条件 desc ) 排名(别名),t.* from 表名

3:序号连续,不管值是否相同
select row_number()over(order by 条件 desc ) 排名(别名),t.* from 表名
K:集合运算
a:并集(union)
语法: //加上union就是说前面的语句和后面语句相同的部分也会显示.
select * from 表名 where 条件 union all select * from 表名 where 条件
b:交集(intersect)
语法: //加上intersect就说两条语句执行后相同的部分显示.
select * from 表名 where 条件 intersect select * from 表名 where 条件
c:差集(minus)
语法: //加上minus就是说执行前一条语句减后一条语句得出的结果显示
select * from 表名 where 条件 minus select * from 表名 where 条件





  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值