sql查询语句, 函数 及索引

来源:互联网 发布:软件开发视频大讲堂 编辑:程序博客网 时间:2024/05/17 03:27

一 查询语句:

一、select 语句
1. * 代表所有列
2. distinct 去除重复的记录
3. ,代表多列数据分隔
4. as 取别名
5. 表达式,和null运算会变为null
6.where  > >= = < <= <>
  between and
  in()
  like   %(任意字符) _(一个字符)  转义like '%\%%' escape '\';
  is null  不能用 =null
 
7. order by   asc  desc

参考D33996-9i-IntroduceSQL-PPT

二、分组统计语句
查询的所有原始列数据必须在分组里边,统计函数不要求
统计函数不统计null值
select  deptno,
  sum(sal) 部门汇总,
  count(1) 人数,
  max(sal) 土豪,
  min(sal) 土鳖,
  avg(sal) 平均
from emp group by deptno;

二 函数:

例如:
--1、字符函数
select lower('Ling and LINGLI') from dual;   --小写
select upper('Ling and LINGLI') from dual;   --大写
select initcap('Ling and LINGLI') from dual; --单词首字母大写

select concat('Ling ','LINGLI') from dual;      --连接
select substr('Ling and lingli',1,3) from dual;--取子字符串,索引号从1开始
select length('Ling and LINGLI') from dual;    --长度
select instr('Ling and LINGLI','om') from dual;--搜索字符串的位置
select lpad('1',5,'0') from dual;           --左边补齐到N个长度的字符
select rpad('1',5,'0') from dual;           --右边补齐到N个长度的字符

select trim('   ling') from dual;         --删除两边的空白
select ltrim('   ling')from dual;         --删除左边的空白
select rtrim('   ling')from dual;         --删除右边的空白
select ltrim('$$$123,456.01','$')from dual; --删除左边的指定的字符

select replace('$$$123,456.01','$','&')from dual;  --删除左边的指定的字符
--2.数字函数
select round(123.254,2) from dual;  --四舍五入
select trunc(123.256,2) from dual;  --截取数据
select mod(10.5,3 )     from dual;  --取余数
select abs(-15)         from dual;  --取绝对值
select sign(0)          from dual;  --取符号值,正数(1),负数(-1),零(0)
select ceil(15.5)       from dual;  --向上取整数,大于等于他的一个整数
select floor(15.5)      from dual;  --向下取整数,小于等于他的一个整数
--3.日期函数
select sysdate       from dual;  --当前系统时间
select sysdate+1     from dual;  --当前系统时间+天数
select next_day(sysdate,'星期一')     from dual;   --下个星期一
select last_day(sysdate)              from dual;   --最后一天
select add_months(to_date('2011-1-30','yyyy-mm-dd'),1)     from dual;   --加一个月
--月份之间的差距
select months_between(to_date('2011-08-30','yyyy-mm-dd'),to_date('2011-01-30','yyyy-mm-dd')) from dual;
--天的差距
select sysdate-to_date('2015-8-1','yyyy-mm-dd') from dual;

--4.转型函数
select to_char(sysdate,'yyyy"年"mm-dd hh:mi:ss pm')  from dual;--转换时间格式
select to_char(1150.25,'999,999.9999')  from dual;             --转换数字格式
select to_char(1150.25,'L000,000.0000')  from dual;            --转换数字格式

select to_number('¥001,150.2500','L000,000.0000')  from dual;--转换数字格式
select to_date('2011年10-13 12:21:11 下午','yyyy"年"mm-dd hh:mi:ss pm')  from dual;--转换日期格式

--其他函数
select nvl(null,0) from dual;     --将空值替换为指定的值
select nvl(10,0) from dual;     --将空值替换为指定的值

select nvl2(null,1,0) from dual;    --判断是为空,不为空用前面的指定值,为空用后面的指定值
select nvl2(1000,1,0) from dual;    --判断是为空,不为空用前面的指定值,为空用后面的指定值

select nullif(1,1) from dual;    --判断是否相等,等就返回空值null
select nullif(1,2) from dual;    --判断是否相等,不等就返回前面的值

--返回第一个非空值
select coalesce(null,10) from dual;
select coalesce(null,null,10) from dual;
select coalesce(null,null,null,10) from dual;
select coalesce(null,null,null,10,20) from dual;

--多分支判断返回第一个非控制
select decode('条件值1','条件值1','结果值1','条件值2','结果值2','条件值3','结果值3','默认值') from dual;
select decode('条件值2','条件值1','结果值1','条件值2','结果值2','条件值3','结果值3','默认值') from dual;
select decode('条件值N','条件值1','结果值1','条件值2','结果值2','条件值3','结果值3','默认值') from dual;

三 索引:

create index ix_student_name on student(name);
create unique index ix_student_name on student(name);
create bitmap index ix_student_sex on student(sex);
drop index ix_student_name;

0 0
原创粉丝点击