【创作赢红包】sql常用语法
1、限定返回得行(取emp中rownum=2得记录)
select *
from (select a.*,rownum as rn from emp a where rownum <= 2)
where rn = 2;
2、从表中随机返回几条记录
with t as
(select * from emp order by dbms_random.value)
select * from t where rownum <= 3;
为什么要嵌套一层?来看下不嵌套得结果:
select * from emp where rownum <= 3 order by dbms_random.value;
多执行几次这个sql:
取其中两次得结果来看,虽然empno=7521得数据位置发生变化了,但是执行了那么多次,就这三条数据换了位置而已,并没
加进来其他记录。所以说,正确的做法是:先随机排序,再取数。
3、转译特殊字符
with t as (
select 'ZHAO_YFD' as a from dual
union
select 'YAN%D' as a from dual
)
select * from t where t.a like '%\_%' escape '\';
上面sql如果不加escape做转译,那么这个sql查询结果为空。
4、字符替换函数之:translate
select translate('zhaoyandong zhaoyandong','yan','666') from dual;
--结果为:zh6o666do6g zh6o666do6g
select translate('zhaoyandong zhaoyandong','yan','6') from dual;
--结果为:zho6dog zho6dog
select translate('zhaoyandong zhaoyandong','yan','') from dual;
--结果为:null
从上面三个测试知道,translate函数是以字符为单位来进行替换,而且当要替换的字符范围‘yan’在后面to_string中存在那么正常全部挨个替换,
如果在to_string中的字符与from_srting不能相匹配,少得字符在源字符串中被更新为null值,如果不存在to_string,那么整个源字符串整个替换
为null。
5、某一列数据为字母和数字混合,根据此列中的数字进行排序
with t as (
select 'SDF1' as a from dual
union
select 'D3' as a from dual
union
select 'YA5' as a from dual
union
select 'YA2D' as a from dual
union
select 'YAN4D' as a from dual
)
select a,translate(a,'$ABCDEFGHIGKLMNOPQISTUVWXYZ','$' )as info from t order by 2;
没错,可以使用上面translate函数得特性来做,在from_string中加入一个a列不存在得字符,在to_String字符串中只映射$为$,这样子能把所有的
字母都替换为null,并且还不会让整个源字符串为空。
6、领导对工资在1-2000范围内的用户感兴趣,让他们得信息排在前面便于领导观看,其他不太关心得薪资范围数据在他们后面显示
select empno, ename, sal
from emp
order by case
when sal >= 1000 and sal <= 2000 then
1
else
2
end asc,
sal asc;
这类写法我是放在order by中了,也可以通过case when 新加一个附加列,根据附加列进行排序。
7、union all 与空字符串
有时候union all时候,可能两个需要union得结果集列数不同,这时如果列类型适合(比如缺得列类型为varchar),可以用空字符串来填充列,如下例子:
select empno,mgr from emp where deptno=10 and mgr is null
union all
select empno, 1 from emp where deptno=20;
这是没问题得,但是如果这样改写:
select empno,mgr from emp where deptno=10 --and mgr is null
union all
select empno, '' from emp where deptno=20;
会报:
但是如果这样来:
select empno,null from emp where deptno=10 and mgr is null
union all
select empno, 'aa' from emp where deptno=20;
select empno,null from emp where deptno=10 and mgr is null
union all
select empno, 1 from emp where deptno=20;
上面得两个sql就不会报错,所以说:空字符串不等于null值。也可以用case when 来判断测试。空字符串是varchar类型,所以报错得那个sql中,number
类型得mgr列和空字符串union肯定报数据类型问题,而null可以是任何不同类型,所以上面得这两个sql是没问题得。
8、union 与or
如果条件里面有or的话,经常改写成union,用union也要注意一些细节。
--1、先创建测试表:
create table emp_test as select * from emp;
create index idx_emp_empno on emp_test(empno);
create index idx_emp_ename on emp_test(ename);
--2、执行下面查询:
select empno,ename from emp_test where empno=7788 or ename ='SCOTT';
--结果:
EMPNO ENAME
7788 SCOTT
--如果改成union all就是错的
select empno,ename from emp_test where empno=7788
union all
select empno,ename from emp_test where ename ='SCOTT';
EMPNO ENAME
7788 SCOTT
7788 SCOTT
--因为原来语句中条件为or,是两个结果得合集而不是并集,所以一般改写时用union来去掉重复数据:
select empno,ename from emp_test where empno=7788
union --all
select empno,ename from emp_test where ename ='SCOTT';
EMPNO ENAME
7788 SCOTT
用union改写后能使原来用or得sql走的全表扫描变成走索引扫描,可以看下执行计划,在看执行计划之前
先把位图转换得影响(上面用union改写得这个sql,这个情况下很容易把B Tree索引先做个位图转换为位图索引
然后做完位操作后再通过rowid去回表查数,导致查询效率降低)给取消,可以通过下面操作来禁用bitmap convert:
SQL> alter session set "_b_tree_bitmap_plans"=false;
会话已更改。
接着来看执行计划:
SQL> set autotrace traceonly;---预估执行计划
SQL> select empno,ename from emp_test where empno=7788 or ename ='SCOTT';
执行计划
----------------------------------------------------------
Plan hash value: 3124080142
------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 20 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| EMP_TEST | 1 | 20 | 3 (0)| 00:00:01 |
------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("EMPNO"=7788 OR "ENAME"='SCOTT')
----用or得sql可以看到走的是全表扫描,接下来看看union改写得:
【创作赢红包】sql常用语法相关推荐
- sql常用语法命令及函数_SQL右连接命令:语法示例
sql常用语法命令及函数 For this guide we'll discuss the SQL RIGHT JOIN. 对于本指南,我们将讨论SQL RIGHT JOIN. 正确加入 (Right ...
- SQL常用语法网络整理版本
下列语句部分是Mssql语句实现: SQL分类: DDL-数据定义语言(CREATE,ALTER,DROP,DECLARE) DML-数据操纵语言(SELECT,DELETE,UPDATE,INSER ...
- 【创作赢红包】CSDN博客创作灵感
<CSDN博客创作灵感> 现在,解决CSDN博客创作灵感的问题,是非常非常重要的. 所以, CSDN博客创作灵感,到底应该如何实现. 这种事实对本人来说意义重大,相信对这个世界也是有一定意 ...
- 【创作赢红包】Python第3章 流程控制
这里写目录标题 [本章导读] 真值测试 比较运算 成员运算 for循环 while循环 项目实训1 项目实训2 项目实训3 项目实训4: 项目实训5: 项目实训6: 项目实训7: 项目实训8 项目实训 ...
- 【创作赢红包】Git Merge 深入解析
目录 前言 一.Git Merge 基本概念 1.1 Git 分支 1.2 Git Merge 简介 1.3 合并冲突 二.Git Merge 实践操作 2.1 创建和切换分支 2.2 合并分支 2. ...
- 【创作赢红包】如何提高写作水平?让写作成为你的强项。
在今天这个信息爆炸的世界中,写作已经成为了一种必备的技能.不论你是学生.职场人还是自由职业者,良好的写作能力都将让你事半功倍.但是如何提高写作水平呢?以下是几个有效的方法: 多读多写 要想成为一名优秀 ...
- postgre sql常用语法
删除表格 DROP TABLE table_name; 新增字段 ALTER TABLE IF EXISTS table_name ADD COLUMN column_name datatype; d ...
- 【创作赢红包】Prompt Learning的基本思想和常用方法
- Spark SQL概述,DataFrames,创建DataFrames的案例,DataFrame常用操作(DSL风格语法),sql风格语法
一. Spark SQL 1. Spark SQL概述 1.1. 什么是Spark SQL Spark SQL是Spark用来处理结构化数据的一个模块,它提供了一个编程抽象叫做DataFrame并且作 ...
最新文章
- Hadoop十岁!Doug Cutting成长史+他眼中大数据技术的未来
- ARCore中根据屏幕坐标计算射线的算法
- python装饰器-Python基础-20装饰器
- 数据绑定控件之Repeater
- quick-cocos2dx在eclipse下的lua调试
- XGBoost相关知识-1
- 【测试】物料帐单/多级差异流转测试
- 输入 ng build 或者 ng serve 之后没有任何输出的问题分析
- html5 字母索引,jQuery建立一个按字母顺序排列的友好页面索引(兼容IE6/7/8)
- 手动修改Icon图标的颜色深度(张佩)
- 云清联盟发布全网协同防护方案 2017将继续“纳新”共赢未来
- css实现实心三角箭头,兼容ie
- Python爬取电影天堂最新发布电影下载地址
- 软件License设计思路与实现方案
- conda SSL错误 SSLError,爬虫应用总结
- 济南公积金 销户 提取
- laravel 框架maatwebsite/excel拓展导出excel增加sheets分页功能
- java字符串Emoji表情的处理
- 协助医学未来的10项有前途的技术
- 使用scikit-image feature计算图像特征与常见特征示例