ORACLE学习笔记--基本select语句、函数、多表关联
select name,age from users;
select name,age from users where age>20;
select a.name,a.age,b.sex,b.pay from users a,salary b where a.name=b.name;
select * from users;
select 语句不区分大小写,可以跨行。
关键字不可以简写和跨行。
字符和日期列居左,数字列居右。
关键字不可以简写和跨行。
字符和日期列居左,数字列居右。
+ - * /运算,有()改变优先级
select age+5 from users;
select age+5 from users;
null与任何值运算都为null
定义别名
select name as xingming from users;
select name as xingming from users;
用双引号不改变别名大小写和空格
select name "XINGMING" from users;
select name "XINGMING" from users;
连接字符
select name||'is'||age from users;
select name||'is'||age from users;
排除重复的行
select distince age from users;
select distince age from users;
select中引号内区分大小写
select name from users where name like 'W%'
select name from users where name like 'W%'
比较表达式
betweeb and 大于等与,小于等于
in()
like 一个'_a' 多个like '%a'
is null
betweeb and 大于等与,小于等于
in()
like 一个'_a' 多个like '%a'
is null
逻辑条件
and
or
not
and
or
not
排序
默认是升序
select * from users order by age
select * from users order by age desc
select * from users order by 1
单行函数
select sum(age) from users
select substr(name,1,2) from users
select substr(name,-1,2) from users;负数表示从右边第几个开始
lower()大写转小写,upper()小写转大写,initcap()第一个字母大写,其他小写,concat()两个字符连在一起
instr(name,'A')A在name中的第几个位置
lpad(name,10,'*')不够10位左边以*填充
rpad(name,10,'$')
trim('H' from 'hello')从hello中去掉h,去掉空格
round(45.332,2)四舍五入,两位小数
trunc(45.332,2)不四舍五入,两位小数
mod(130,3)求余数
dual是一个虚的表,可以用来从函数和计算中查看结果。
select substr(name,1,2) from users
select substr(name,-1,2) from users;负数表示从右边第几个开始
lower()大写转小写,upper()小写转大写,initcap()第一个字母大写,其他小写,concat()两个字符连在一起
instr(name,'A')A在name中的第几个位置
lpad(name,10,'*')不够10位左边以*填充
rpad(name,10,'$')
trim('H' from 'hello')从hello中去掉h,去掉空格
round(45.332,2)四舍五入,两位小数
trunc(45.332,2)不四舍五入,两位小数
mod(130,3)求余数
dual是一个虚的表,可以用来从函数和计算中查看结果。
日期缺省显示是DD-MON-RR
select sysdate from dual;
select (sysdate-birthday)/7 from users;多少个星期
select months_between(sysdate,order_date) from users;间隔多少个月
select add_months(sysdate,4) from dual;当前时间加4个月
select next_day(sysdate,'sunday') from dual;当前时间的下个星期天是什么时候
select last_day(sysdate) from dual;当前月的最后一天
假设当前时间是2006-09-25
round(sysdate,'month')----01-10-06
round(sysdate,'year')-----01-01-07
trunc(sysdate,'month')----01-09-06
trunc(sysdate,'year')-----01-01-06
round(sysdate,'month')----01-10-06
round(sysdate,'year')-----01-01-07
trunc(sysdate,'month')----01-09-06
trunc(sysdate,'year')-----01-01-06
转换函数
隐式数据类型转换
varchar2 or char to number
varchar2 or char to date
number to varchar2
date to varchar2
varchar2 or char to number
varchar2 or char to date
number to varchar2
date to varchar2
显式数据类型转换
to_char(date,'dd-mm-yyyy')
to_char(number,'$9999'),9表示一个数字,0占位,$美圆符号,L本地货币符号,。小数,,千位指示
to_number(char)
to_date(char,'dd-mm-yyyy')
to_char(number,'$9999'),9表示一个数字,0占位,$美圆符号,L本地货币符号,。小数,,千位指示
to_number(char)
to_date(char,'dd-mm-yyyy')
函数是可以嵌套的
substr(name,length(name),1)
通用函数
nvl(name,0)name为空则显示0,不为空则显示原值
nvl2(name,0,1)name不为空,显示0,为空,显示1
nullif(length(name),length(address))相等则返回null,不相等则返回第一个值
coalesce(name,age,10)显示第一个不为空的值
decode(age,10,1,20,2,30,3)
case then else end
nvl2(name,0,1)name不为空,显示0,为空,显示1
nullif(length(name),length(address))相等则返回null,不相等则返回第一个值
coalesce(name,age,10)显示第一个不为空的值
decode(age,10,1,20,2,30,3)
case then else end
多表数据访问
select a1.id,a1.name,a2.age from student a1,teather a2 where a1.id=a2.id
不给where条件得到的是一个笛卡儿集
不给where条件得到的是一个笛卡儿集
等值连接(主键和外键之间)
多个条件用and子句
多个条件用and子句
两个表有相同的列名,用表的别名加以区分
select a.name,b.name from student a,teather b where a.id=b.id
连接N张表,至少有N-1个连接条件
非等值连接
等值连接、非等值连接找出的结构都是相互匹配的
外连接
不满足条件的结果也反映出来
不满足条件的结果也反映出来
select a.name,a.age,b.pay from student a,teacher b where a.id(+)=b.id
(+)出现在右边,左连接
(+)出现在左边,右连接
(+)出现在左边,右连接
自连接
select * from student s ,student t where s.id=t.id;
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/25556429/viewspace-1049628/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/25556429/viewspace-1049628/