ORACLE的查询语句
oracle的select查询语句(DQL):
语法:
select //查询动作关键字
[distinct|all] //描述列表字段中的数据是否去除记录
select_list //需要查询的字段列表,占位符,可以是多个
from table_name //from关键字,表数据来源,表或者视图
[where_clause] // WHERE 条件部分
[group_by_clause] // 分组条件
[HAVING condition] // 分组的聚合函数
[order_by_clause] // 排序条件
1.字段查询
.1获取指定字段的数据
()普通查询特定字段
SELECT productid,productname,productprice from productinfo;
()根据当前模式获取表或视图的列
select scott.productinfo.productid,productinfo.productname,productinfo.productprice from scott.productinfo;
()获取所有字段的数据
SELECT * from productinfo;
使用*号的弊端:
()执行效率没有明确查询列高;
()增加不必要的网络消耗;
()在表中增加新的字段时,会造成一些不必要的程序异常。 .2使用别名替代表中的字段名:
select productid 产品编号, productname AS 产品名称, productprice AS 产品价格 FROM PRODUCTINFO ; .3使用表达式操作查询的字段:
select productid,productname,productprice || '*' 1.25 || '=' || productprice*1.25 as new_productprice
from productinfo; .4使用函数操作查询的字段:
select productid 产品编号,subStr(productid,,) as 截取后的编号,productname as 产品名称,productprice as 产品价格
from productinfo; .5去除检索数据中的重复记录:
select distinct category 产品类型 from productinfo ;
2.数据处理
.1检索出来的数据排序:
在select语句的后面,不管有什么条件限制,排序只能在最后一项。
语法:
order by
{expr | position | c_alias}
[ASC | DESC]
[NULLS FIRST | NULLS LAST]
[,{expr | position | c_alias}
[ASC | DESC]
[NULLS FIRST | NULLS last]
]...
.2使用升序,降序来处理数据
select productname, quantity from productinfo order by quantity; .3排序时对null值得处理
升序时null值在首位:
select productname, quantity from productinfo order by quantity nulls first;
降序时null值在末尾:
select productname, quantity from productinfo order by quantity nulls last ;
.4使用别名作为查询手段
select productname 产品名称,quantity 产品数量 from productionfo order by 产品数量 nulls last;
2.5 使用表达式作为排序字段
select productname,productprice,quantity,productprice*quantity from productinfo order by productprice*quantity ASC;
备注:null值和其他值相乘相加时,结果还是null;
2.6 使用字段的位置作为排序字段
select productname,productprice,quantity from productinfo order by desc;
.7使用多个字段排序:
select productname,category,quantity from productinfo order by category asc , desc nulls last; .where 设置检索条件
关系操作符:<,<=,>,>=,=,!=,<>.
比较操作符:is null, like,between...end...,in
逻辑操作符:and, or, not
.1单一条件检索:
where quantity>
.2多个条件:
where productprice>= and productprice<=;
where productprice between end ;
.3模糊查询:
%:代替多个字符
_:代替一个字符
例:where productname like '%三星%'
.4查询条件限制在某个列表范围之内
where category in('','')
.5针对null值得查询
where quantity is null ;
where quantity is not null; 4分组查询:group by和having
.1group by:
语法:GROUP BY
{ expr /**数据库列名**/
| {rollup | cube}({expr [,expr ]...}) /**group by子句的扩展,可以返回小计和总计记录**/
}
一个字段分组:
select category, avg (productprice) 平均价格 from productionfo group by category;
多个字段分组:
select category 产品类型编码,avg(productprice) 平均价格,origin 产地 from productinfo group by category,origin;
where 子句和分组混用(先筛选在分组):
select category,avg(productprice) 平均价格,origin from productinfo
where productprice > group by category,origin;
注意:当查询中出现group by子句时,select列表中只能存在分组函数,或出现在group by子句中的字段。
.2HAVING
having子句通常和group by子句一起使用,限制搜索条件,对group by子句负责。
select category,avg(productprice) 平均价格 from productinfo group by category HAVING avg(productprice)>; 5子查询
嵌套在另外一个语句中的select语句,本质上是where后的一个条件表达式。
5.1 子查询返回单行
单一条件子查询:
select productname,productprice
from productinfo
where category=(select category from categoryinfo where category = 'MP3'); 多个条件查询:
select productname,productprice from productinfo
where productprice >(select min(productprice) from productinfo)
and productprice<(select max(rpductprice) from productinfo)
.2子查询返回多行
in:符合任一个都可以
例:select productname,productprice from productinfo
where category in
(select categoryid from categoryinfo where categoryname = '电视' or categoryname = 'MP3');
*any:表示满足子查询结果的任何一个,和<,<=搭配,表示小于等于列表中的最大值,和>,>=配合表示大于等于列表中的最小值。
例:select productname,productprice from productinfo where productprice <
any (select productprice from productinfo where category = '')
and category <> ''
some:和any差不多,用在非'='的环境中,表示找出和子查询中任何相当的数据:
例:select productname,productprice from productinfo where productprice =
some (select productprice from productinfo where category = '') and category <> '';
*all:表示满足子查询结果的所有结果。和<,<=搭配,表示小于等于列表中的最小值;而和>,>=搭配时表示大于等于列表中的最大值。
例:select productname,productprice from productinfo where productprice <
all(select productprice from productprice from productinfo where category = ''); .连接查询:(内连接,外连接,全连接,自连接)
.1最简单的连接查询,用‘,’ (笛卡尔积,不推荐)
select * from productinfo,categoryinfo;
.2内连接:(两张表或多张表简单连接,只查询出匹配的记录)
等值连接:‘=’连接
例:select p.productname,p.productproce,c.categoryname
from productinfo p, categoryinfo c
where p.category = c.categoryid;
或:select p.productname,p.productprice,c.categoryname
from productinfo p inner join categoryinfo c
on p.category = c.categoryid;
不等值连接:'>','>=','<=','<','!=','<>','between...and...','in'
例:select p.productname,p.productprice,c.categoryname
from productinfo p inner join categoryinfo c on p.category in c.categoryid;
或:select p.productname,p.productprice,c.categoryname
from productinfo p, categoryinfo c where p.category in c.categoryid;
.3自连接:自身引用作为另一张表来处理
例:select p.productname,p.productprice,pr.productname,pr.productprice,pr.quantity
from productinfo p, productingo pr
where p.productid != pr.productid
and p.quantity = pr.quantity
and p.rowid < pr.rowid;
或:select p.productname,p.productprice,pr.productname,pr.procudtprice,pr.quantity
from productinfo p join procudtinfo pr
on p.productid != pr.productid
and p.quantity = pr.quantity
and p.rowid < pr.rowid;
.4外连接:((+)的使用:该操作符总是放在非主表的一方,并且使用where子句,左右连接可用)
左外连接:包含左表全部记录和右表匹配记录
例:select p.productname,p.productprice,p.category,c.categoryid,c.categoryname
from productinfo p left join categoryinfo c on p.category = c.categoryid;
或:select p.productname,p.productprice,p.category,c.categoryid,c.categoryname
from productinfo p , categoryinfo c
where p.category = c.categoryid(+);
右外连接:包含右表全部记录和左表匹配记录
例:select p.productname,p.productprice,p.category,c.categoryid,c.categoryname
from productinfo p right join categoryinfo c on p.category = c.categoryid;
或:select p.productname,p.productprice,p.category,c.categoryid,c.categoryname
from productinfo p,categoryinfo c
where p.category(+) = c.categoryid;
全外连接:所有匹配成功的记录,和左右表中未匹配成功的记录
例:select p.productname,p.procudtprice,p.category,c.categoryid,c.categoryname
from productinfo p full join categoryinfo c on p.category = c.categoryid;
ORACLE的查询语句的更多相关文章
- Oracle分页查询语句的写法(转)
Oracle分页查询语句的写法(转) 分页查询是我们在使用数据库系统时经常要使用到的,下文对Oracle数据库系统中的分页查询语句作了详细的介绍,供您参考. Oracle分页查询语句使我们最常用的 ...
- 各种oracle参数查询语句
各种oracle参数查询语句 1.show parameter:--显示各个系统参数配置 2.select * from v$parameter;--显示各个系统参数配置 2.show paramet ...
- Oracle分页查询语句的写法
分页查询是我们在使用数据库系统时经常要使用到的,下文对Oracle数据库系统中的分页查询语句作了详细的介绍,供您参考. AD:2013云计算架构师峰会精彩课程曝光 Oracle分页查询语句使我们最常用 ...
- ORACLE中查询语句的执行顺及where部分条件执行顺序测试
Oracle中的一些查询语句及其执行顺序 原文地址:https://www.cnblogs.com/likeju/p/5039115.html 查询条件: 1)LIKE:模糊查询,需要借助两个通配符, ...
- 45 个非常有用的 Oracle 日期查询语句
日期/时间 相关查询 获取当前月份的第一天 运行这个命令能快速返回当前月份的第一天.你可以用任何的日期值替换 “SYSDATE”来指定查询的日期. SELECT TRUNC (SYSDATE, 'MO ...
- oracle 常用查询语句
一.一般日常用的脚本 1.检查源库每个节点至少3组redoselect group#,thread#,bytes/1024/1024,members,status from v$log; select ...
- oracle数据库查询语句case的用法
实现功能: 1.先查询status=2的记录,如果查询到记录则返回第一条记录的Product_Name:2.如果查询不到status=2的记录,则判断status=1的记录是否存在,不存在则返回“请耐 ...
- Oracle ->> 层级查询语句(hierarchical query)connect by
Oracle中的Connect By... Start With语句实现了递归查询或者树状查询. Connect By Prior 一方为起始(root)的ID 参考: http://www.360d ...
- Oracle数据库查询语句
编写以下查询的SQL语句,以scott用户的emp表和dept表作为查询数据: 1.列出至少有一个员工的所有部门. SQL语句: select * from SCOTT.DEPT where dept ...
随机推荐
- Linux3基本命令 ls,pwd,cat,echo,mv,cp,mkdir,rm,ln
ls 列出文件名称. -l 列出长文件名称. -rwxr-xr-- 1 root root 10739 Dec 23 13:31 bbscon (7) (4) (5) (6) ...
- CodeForces - 721E
题目大意 现有一个长为 L的数轴,你要从0走到 L 给出n个互不相交的可行域. 你要选择长度为p的段,要求每一个段都要在可行域内. 选完一段之后下一段要么和其相接,要么和其间距至少为t,求问最多能选择 ...
- 在英文Win7操作系统上部署C#开发的Web系统出现乱码的解决方法
今天,迁移机器,把一个使用C#开发的Web系统部署到一台英文版Win7操作系统上,部署好以后,系统可以登录,只是网页上出现汉字乱码. 在这台电脑上,打开Word等文本编辑器,是可以正常输入.显示中文的 ...
- Ubuntu14.04下安装glog
下载原始代码编译 1. Clone Source Code glog git clone https://github.com/google/glog 2. Install dependencies ...
- pandas 中的 多条件分割, list 排序
main_comment_num_3m and avg_group_order_cnt_12m = 0.863230main_comment_score_1m and avg_group_order_ ...
- Why does my Authorize Attribute not work?
the roles of a ClaimsPrincipal are actually just claims create with a type of ClaimsIdentity.RoleCla ...
- How to Choose the Best Way to Pass Multiple Models in ASP.NET MVC
Snesh Prajapati, 8 Dec 2014 http://www.codeproject.com/Articles/717941/How-to-Choose-the-Best-Way-to ...
- JQuery解决事件动画重复问题
开发项目时,经常要写动画效果,有时候会遇到动画重复问题,例如:当鼠标移动到某个元素上时,执行某个动画,当我鼠标多次移动到该元素时,该动画就要连续执行,那么怎么去解决呢? 话不多说,直接添代码,简单明了 ...
- UIView的alpha、hidden和opaque属性之间的关系和区别[转]
UIView的alpha.hidden和opaque属性之间的关系和区别 作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/article/details/ ...
- 编写高质量代码改善C#程序的157个建议——建议35:使用default为泛型类型变量指定初始值
建议35:使用default为泛型类型变量指定初始值 有些算法,比如泛型集合List<T>的Find算法,所查找的对象可能会是值类型,也有可能是引用类型.在这种算法内部,我们常常会为这些值 ...