1. oracleselect查询语句(DQL):
  2. 语法:
  3. select //查询动作关键字
  4. [distinct|all] //描述列表字段中的数据是否去除记录
  5. select_list //需要查询的字段列表,占位符,可以是多个
  6. from table_name //from关键字,表数据来源,表或者视图
  7. [where_clause] // WHERE 条件部分
  8. [group_by_clause] // 分组条件
  9. [HAVING condition] // 分组的聚合函数
  10. [order_by_clause] // 排序条件
    1.字段查询
  11. .1获取指定字段的数据
  12. ()普通查询特定字段
  13. SELECT productid,productname,productprice from productinfo;
  14. ()根据当前模式获取表或视图的列
  15. select scott.productinfo.productid,productinfo.productname,productinfo.productprice from scott.productinfo;
  16. ()获取所有字段的数据
  17. SELECT * from productinfo;
  18. 使用*号的弊端:
  19. ()执行效率没有明确查询列高;
  20. ()增加不必要的网络消耗;
  21. ()在表中增加新的字段时,会造成一些不必要的程序异常。
  22.  
  23. .2使用别名替代表中的字段名:
  24. select productid 产品编号, productname AS 产品名称, productprice AS 产品价格 FROM PRODUCTINFO ;
  25.  
  26. .3使用表达式操作查询的字段:
  27. select productid,productname,productprice || '*' 1.25 || '=' || productprice*1.25 as new_productprice
  28. from productinfo;
  29.  
  30. .4使用函数操作查询的字段:
  31. select productid 产品编号,subStr(productid,,) as 截取后的编号,productname as 产品名称,productprice as 产品价格
  32. from productinfo;
  33.  
  34. .5去除检索数据中的重复记录:
  35. select distinct category 产品类型 from productinfo ;
    2.数据处理
  36. .1检索出来的数据排序:
  37. select语句的后面,不管有什么条件限制,排序只能在最后一项。
  38. 语法:
  39. order by
  40. {expr | position | c_alias}
  41. [ASC | DESC]
  42. [NULLS FIRST | NULLS LAST]
  43. [,{expr | position | c_alias}
  44. [ASC | DESC]
  45. [NULLS FIRST | NULLS last]
  46. ]...
  47. .2使用升序,降序来处理数据
  48. select productname, quantity from productinfo order by quantity;
  49.  
  50. .3排序时对null值得处理
  51. 升序时null值在首位:
  52. select productname quantity from productinfo order by quantity nulls first;
  53. 降序时null值在末尾:
  54. select productname, quantity from productinfo order by quantity nulls last ;
  55. .4使用别名作为查询手段
  56. select productname 产品名称,quantity 产品数量 from productionfo order by 产品数量 nulls last;
  57. 2.5 使用表达式作为排序字段
  58. select productname,productprice,quantity,productprice*quantity from productinfo order by productprice*quantity ASC;
  59. 备注:null值和其他值相乘相加时,结果还是null;
  60. 2.6 使用字段的位置作为排序字段
  61. select productname,productprice,quantity from productinfo order by desc;
  62. .7使用多个字段排序:
  63. select productname,category,quantity from productinfo order by category asc , desc nulls last;
  64.  
  65. .where 设置检索条件
  66. 关系操作符:<,<=,>,>=,=,!=,<>.
  67. 比较操作符:is null, like,between...end...,in
  68. 逻辑操作符:and, or, not
  69. .1单一条件检索:
  70. where quantity>
  71. .2多个条件:
  72. where productprice>= and productprice<=;
  73. where productprice between end ;
  74. .3模糊查询:
  75. %:代替多个字符
  76. _:代替一个字符
  77. 例:where productname like '%三星%'
  78. .4查询条件限制在某个列表范围之内
  79. where category in('','')
  80. .5针对null值得查询
  81. where quantity is null ;
  82. where quantity is not null;
  83.  
  84. 4分组查询:group byhaving
  85. .1group by
  86. 语法:GROUP BY
  87. { expr /**数据库列名**/
  88. | {rollup | cube}({expr [,expr ]...}) /**group by子句的扩展,可以返回小计和总计记录**/
  89. }
  90. 一个字段分组:
  91. select category, avg (productprice) 平均价格 from productionfo group by category;
  92. 多个字段分组:
  93. select category 产品类型编码,avg(productprice) 平均价格,origin 产地 from productinfo group by category,origin;
  94. where 子句和分组混用(先筛选在分组):
  95. select category,avg(productprice) 平均价格,origin from productinfo
  96. where productprice > group by category,origin;
  97. 注意:当查询中出现group by子句时,select列表中只能存在分组函数,或出现在group by子句中的字段。
  98. .2HAVING
  99. having子句通常和group by子句一起使用,限制搜索条件,对group by子句负责。
  100. select category,avg(productprice) 平均价格 from productinfo group by category HAVING avg(productprice)>;
  101.  
  102. 5子查询
  103. 嵌套在另外一个语句中的select语句,本质上是where后的一个条件表达式。
  104. 5.1 子查询返回单行
  105. 单一条件子查询:
  106. select productname,productprice
  107. from productinfo
  108. where category=(select category from categoryinfo where category = 'MP3');
  109.  
  110. 多个条件查询:
  111. select productname,productprice from productinfo
  112. where productprice >(select min(productprice) from productinfo)
  113. and productprice<(select max(rpductprice) from productinfo)
  114. .2子查询返回多行
  115. in:符合任一个都可以
  116. 例:select productname,productprice from productinfo
  117. where category in
  118. (select categoryid from categoryinfo where categoryname = '电视' or categoryname = 'MP3');
  119. *any:表示满足子查询结果的任何一个,和<,<=搭配,表示小于等于列表中的最大值,和>,>=配合表示大于等于列表中的最小值。
  120. 例:select productname,productprice from productinfo where productprice <
  121. any (select productprice from productinfo where category = '')
  122. and category <> ''
  123. some:和any差不多,用在非'='的环境中,表示找出和子查询中任何相当的数据:
  124. 例:select productname,productprice from productinfo where productprice =
  125. some (select productprice from productinfo where category = '') and category <> '';
  126. *all:表示满足子查询结果的所有结果。和<,<=搭配,表示小于等于列表中的最小值;而和>,>=搭配时表示大于等于列表中的最大值。
  127. 例:select productname,productprice from productinfo where productprice <
  128. all(select productprice from productprice from productinfo where category = '');
  129.  
  130. .连接查询:(内连接,外连接,全连接,自连接)
  131. .1最简单的连接查询,用‘,’ (笛卡尔积,不推荐)
  132. select * from productinfo,categoryinfo;
  133. .2内连接:(两张表或多张表简单连接,只查询出匹配的记录)
  134. 等值连接:‘=’连接
  135. 例:select p.productname,p.productproce,c.categoryname
  136. from productinfo p, categoryinfo c
  137. where p.category = c.categoryid;
  138. 或:select p.productname,p.productprice,c.categoryname
  139. from productinfo p inner join categoryinfo c
  140. on p.category = c.categoryid;
  141. 不等值连接:'>','>=','<=','<','!=','<>','between...and...','in'
  142. 例:select p.productname,p.productprice,c.categoryname
  143. from productinfo p inner join categoryinfo c on p.category in c.categoryid;
  144. 或:select p.productname,p.productprice,c.categoryname
  145. from productinfo p, categoryinfo c where p.category in c.categoryid;
  146. .3自连接:自身引用作为另一张表来处理
  147. 例:select p.productname,p.productprice,pr.productname,pr.productprice,pr.quantity
  148. from productinfo p, productingo pr
  149. where p.productid != pr.productid
  150. and p.quantity = pr.quantity
  151. and p.rowid < pr.rowid;
  152. 或:select p.productname,p.productprice,pr.productname,pr.procudtprice,pr.quantity
  153. from productinfo p join procudtinfo pr
  154. on p.productid != pr.productid
  155. and p.quantity = pr.quantity
  156. and p.rowid < pr.rowid;
  157. .4外连接:((+)的使用:该操作符总是放在非主表的一方,并且使用where子句,左右连接可用)
  158. 左外连接:包含左表全部记录和右表匹配记录
  159. 例:select p.productname,p.productprice,p.category,c.categoryid,c.categoryname
  160. from productinfo p left join categoryinfo c on p.category = c.categoryid;
  161. 或:select p.productname,p.productprice,p.category,c.categoryid,c.categoryname
  162. from productinfo p , categoryinfo c
  163. where p.category = c.categoryid(+);
  164. 右外连接:包含右表全部记录和左表匹配记录
  165. 例:select p.productname,p.productprice,p.category,c.categoryid,c.categoryname
  166. from productinfo p right join categoryinfo c on p.category = c.categoryid;
  167. 或:select p.productname,p.productprice,p.category,c.categoryid,c.categoryname
  168. from productinfo p,categoryinfo c
  169. where p.category(+) = c.categoryid;
  170. 全外连接:所有匹配成功的记录,和左右表中未匹配成功的记录
  171. 例:select p.productname,p.procudtprice,p.category,c.categoryid,c.categoryname
  172. from productinfo p full join categoryinfo c on p.category = c.categoryid;

ORACLE的查询语句的更多相关文章

  1. Oracle分页查询语句的写法(转)

    Oracle分页查询语句的写法(转)   分页查询是我们在使用数据库系统时经常要使用到的,下文对Oracle数据库系统中的分页查询语句作了详细的介绍,供您参考. Oracle分页查询语句使我们最常用的 ...

  2. 各种oracle参数查询语句

    各种oracle参数查询语句 1.show parameter:--显示各个系统参数配置 2.select * from v$parameter;--显示各个系统参数配置 2.show paramet ...

  3. Oracle分页查询语句的写法

    分页查询是我们在使用数据库系统时经常要使用到的,下文对Oracle数据库系统中的分页查询语句作了详细的介绍,供您参考. AD:2013云计算架构师峰会精彩课程曝光 Oracle分页查询语句使我们最常用 ...

  4. ORACLE中查询语句的执行顺及where部分条件执行顺序测试

    Oracle中的一些查询语句及其执行顺序 原文地址:https://www.cnblogs.com/likeju/p/5039115.html 查询条件: 1)LIKE:模糊查询,需要借助两个通配符, ...

  5. 45 个非常有用的 Oracle 日期查询语句

    日期/时间 相关查询 获取当前月份的第一天 运行这个命令能快速返回当前月份的第一天.你可以用任何的日期值替换 “SYSDATE”来指定查询的日期. SELECT TRUNC (SYSDATE, 'MO ...

  6. oracle 常用查询语句

    一.一般日常用的脚本 1.检查源库每个节点至少3组redoselect group#,thread#,bytes/1024/1024,members,status from v$log; select ...

  7. oracle数据库查询语句case的用法

    实现功能: 1.先查询status=2的记录,如果查询到记录则返回第一条记录的Product_Name:2.如果查询不到status=2的记录,则判断status=1的记录是否存在,不存在则返回“请耐 ...

  8. Oracle ->> 层级查询语句(hierarchical query)connect by

    Oracle中的Connect By... Start With语句实现了递归查询或者树状查询. Connect By Prior 一方为起始(root)的ID 参考: http://www.360d ...

  9. Oracle数据库查询语句

    编写以下查询的SQL语句,以scott用户的emp表和dept表作为查询数据: 1.列出至少有一个员工的所有部门. SQL语句: select * from SCOTT.DEPT where dept ...

随机推荐

  1. libevent源码深度剖析八

    libevent源码深度剖析八 ——集成信号处理 张亮 现在我们已经了解了libevent的基本框架:事件管理框架和事件主循环.上节提到了libevent中I/O事件和Signal以及Timer事件的 ...

  2. PHPMailer fe v4.11 For Thinkphp 3.2

    PHPMailer fe v4.11 For Thinkphp 3.2,你值得拥有! 今晚用TP3.2开发一个东西的时候需要邮件发送功能,理所当然的想到了PHPMailer.于是有了此文!------ ...

  3. c语言实践 用1角 2角 5角 凑成10元钱的方法

    /* 用1角,2角,5角凑出10元钱,有几种办法. 也就是0.1a+0.2b+0.3c=10,化简一下就是 a=100-2b-3c 因为a的范围是0到100,所以弄一个循环 把a的值从0尝试到100, ...

  4. Luogu 2403 [SDOI2010]所驼门王的宝藏

    BZOJ 1924 内存要算准,我MLE了两次. 建立$n + r + c$个点,对于一个点$i$的坐标为$(x, y)$,连边$(n + x, i)$和$(n + r + y, i)$,代表这一列和 ...

  5. Part4_lesson1---Bootloader设计蓝图

    1.bootloader的作用 2.u-boot是bootloader业界的老大 u-boot分为自主模式和开发模式 3.建立U-boot工程 uboot不能在window下面进行解压,因为在wind ...

  6. Part10-C语言环境初始化-Bss段初始化lesson2

    1.BSS段的作用 初始化的全局变量存放在数据段: 局部变量存放在栈中: malloc的存放在堆: 未初始化的全局变量存放在BSS段: 找到bss段的起始与结束地址,往里面添加0,便初始化好了. 打开 ...

  7. 7.linux安全基线加固

    本文大多截图出自于:http://c.biancheng.net/cpp/shell/ 现在大多数企业都是使用linux作为服务器,不仅是linux是开源系统,更是因为linux比windows更安全 ...

  8. HTML、CSS、JavaScript拾遗

    1.html元素中,如果有文本存在,当元素大小不足以容纳文本时,文本会进行强制换行.比如说设置页面不出现滚动条,body的overflow为hidden时,或者scroll为no时,span在超过页面 ...

  9. vba实现excel多表合并

    Excel多表合并之vba实现 需求 保留列名,复制每一个excel里的数据,合并到一个excel 操作步骤 将要合并的文件放在同一文件夹下,复制过来就好(ps:最好不要直接操作原数据文件,避免操作失 ...

  10. .NET MVC对接POLYV——HTML5播放器播放加密视频

    官方参考文档:http://dev.polyv.net/2017/videoproduct/v-playerapi/html5player/html5-docs/ 1.上传视频之前根据自己需要对所上传 ...