Oracle数据库(一)

1、列运算,Oracle数据库在使用sql语句的时候能够进行 + 、- 、 * 、 / 的运算,然后进行输出,例如:
select name , price * 2 from products ;

select to_date('31-AUG-2003') - 3 from dual ;
返回结果:
	TO_DATE('31-AUG-20
	------------------
	28-AUG-03
select to_date('31-JUL-2003') - to_date('31-AUG-2003') from dual ;
返回结果:
	TO_DATE('31-JUL-2003')-TO_DATE('31-AUG-2003')
	---------------------------------------------
	                                          -31

2、列别名的使用
select price * 2 double_price from products ;
返回结果:
DOUBLE_PRICE
------------
39.9
60
51.98
27.9
99.98
29.9
26.98
25.98
21.98
31.98
29.98

	DOUBLE_PRICE
	------------
	       26.98
如果希望在别名中使用空格,并要保留空格,就必须使用双引号("")将别名正名引起来。
select price * 2 "double price" from products ;
	DOUBLE PRICE
	------------
			39.9
			  60
		   51.98
			27.9
		   99.98
			29.9
		   26.98
		   25.98
		   21.98
		   31.98
		   29.98
	
	DOUBLE PRICE
	------------
		   26.98
在使用别名之前,也可以使用可选的关键字AS,如下面的例子所示:
select 10 * (12 / 3 - 1) as "Computation" ;
返回的结果:
	Computation
	-----------
			 30

3、串联操作和并列的输出结果:
看可以使用连接操作来合并列的输出结果,这样允许创建界面更友好,更有意义的输出,例如,可以将customers表中的
first_name 和 last_name 使用连接操作符 || 连接起来,得到完整的顾客的名字。
select first_name || ’ ’ || last_name as “customer name” from customers ;
返回结果:
customer name
-------------------
John Brown
Cynthia Green
Steve White
Gail Black
Doreen Blue
4、控制的检测:
select customer_id,first_name,last_name,dob from customers where dob is null ;
返回结果:
CUSTOMER_ID FIRST_NAME LAST_NAME DOB
----------- ----------- ------------ ----------
4 Gail Black
当表中的某一行的结果为空值时可以使用 is null 来判断是否是为空值。
**在检索所有的行的时候,可以使用 Oracle 的一个内置函数来判断:NVL()。NVL(parameter1,parameter2),paramater1
为要检测的列的名称,parameter2则是用来将parameter1中的空值替换掉的值。
select customer_id,first_name,last_name,nvl(phone,‘unknown phone number’) from customers ;
即将表customers中的phone列中的空置全部替换为 ‘unknown phone number’
NVL()除了用于转换包含空值的字符串列之外,还可以用于转换数字列和日期列,在下面的例子中,NVL()函数用来将dob列中
的空值转换成日期01-JAN-2000;
select customer_id,first_name,last_name,nvl(dob,‘01-JAN-2000’) from customers ;
5、select 查询去重:
对于查询出来的结果重复显示的可以使用 DISTINCT 来去除重复:
select distinct customer_id from purchases ;
返回结果:
CUSTOMER_ID
-----------
1
2
4
3
select customer_id from purchases ;
返回结果:
CUSTOMER_ID
-----------
1
2
3
4
1
2
3
4
3
6、可以使用 where 对子句进行过滤输出
select …
from …
where …
7、使用比较操作符:
在 where 句子中 ,除了等于操作符外,还有很多其他的操作符:
= 等于
<> 或 != 不等于
< 小于
> 大于
<= 小于等于
=> 大于等于
any 与一个列表中的任意值进行比较
all 与一个列表中的所有值进行比较
any 的实际操作:
select * from customers where customer_id > any(1,2,3) ;
返回结果:
CUSTOMER_ID FIRST_NAME LAST_NAME DOB PHONE
----------- -------------- ---------------------- --------
2 Cynthia Green 05-FEB-68 800-555-1212

	          3 Steve            White     16-MAR-71        800-555-1213
	
	          4 Gail             Black                      800-555-1214
	
	          5 Doreen           Blue      20-MAY-70
即只要比给出的要求中的任意一个大就行。
select * from customers where customer_id > all(1,2,3) ;
返回结果:
	CUSTOMER_ID FIRST_NAME    LAST_NAME      DOB                PHONE
	----------- ------------- -----------  ----------------- -----------
	          4 Gail          Black                            800-555-1214
	
	          5 Doreen        Blue           20-MAY-70

8、使用SQL操作符:
1)操作符说明:
LIKE 匹配字符串中的模式
IN 匹配值列表
BETWEEN 匹配值范围
IS NULL 匹配空值
IS NAN 这是Oracle新增的一个操作符。匹配NAN这个特殊值,意思是“非数字”
IS INFINITE 这是Oracle10g新增的一个操作符,匹配无穷的BINARY_FLOAT 和 BINARY_DOUBLE值
还可以使用NOT操作符对LIKE、IN、BETWEEN 和 IS NULL等操作符执行求非运算:
NOT LIKE
NOT IN
NOT BETWEEN
IS NOT NULL
IS NOT NAN
IS NOT INFINITE
2)使用LIKE操作符:
select * from customers
where first_name like ‘_o%’ ;
(1)‘o%'此中的""是占位符,表示在"o"之前有几个字符,"o"表示在第几位起必须是自己指定的字符。
(2)’%o%'这种的表示只要含有自己指定的字母就可以。
select * from customers where first_name not like 'o%’ ;
not 字符的使用
如果需要对一个字符串中的下划线或百分号字符进行文本匹配,可以使用ESCAPE选项。下面的这个例子就会检索出名称
中包含字符串"a_product"的所有产品:
select first_name from customers
where first_name like ‘%a_product%’ escape ‘’ ;
1)escape选项说明在与like操作符一起使用的通配符(本例中是下划线(
))前面的反斜线字符是一个转义字符,
此时就可以对下划线进行文本匹配了,而不会将其当做通配符。
3)使用IN操作符:
在where子句中可以使用in操作符来检索其列值在指定的值列表中的行。下面的select语句使用了in操作符,从customers表中检索
customer_id列的值为2、3 或 5 的记录:
select * from customers
where customer_id in (2 , 3 , 5) ;
如果指定的值列表中有一个为空值,那么not null 就会返回false。
下面的例子说明当not null处理一个包含null的列表时,不会返回任何结果;
select * from customers
where customer_id not in (2,3,5,null) ;
返回结果:
no rows selected
4)使用BETWEEN操作符:
该操作符用来检索列值包含在指定区间内的行。下面的select语句使用了BETWEEN操作符,从customers表中检索
customer_id列的值在 1 和 3 之间的记录:
select * from customers
where customer_id between 1 and 3 ;
9、使用逻辑操作符
X and Y 当 x 和 y 都为 true 时,返回 true
X or Y 当 x 和 y 中有一个为 true 时,就返回 true
NOT x 如果 x 为 false ,则返回 true ;如果 x 为 true ,则返回值为 false
1)select * from customers
where dob > ‘01-JAN-1970’ and customer_id > 3 ;
2) select * from customers
where dob > ‘01-JAN-1970’ or customer_id > 3 ;
10、操作符的优先级
1)如果在同一个表达式中同时使用 and 和 or 操作符,那么 and 的优先级要高于 or (这就意味着要先执行 and 操作);
而比较操作符的优先级高于 and 。当然,可以使用圆括号来改变优先级的顺序。
select* from customers
where dob > ‘01-JAN-1970’
or customer_id < 2
and phone like ‘%1211’ ;
这个sql语句等价于下面的sql语句的执行顺序。
select* from customers where dob > ‘01-JAN-1970’ or (customer_id < 2 and phone like ‘%1211’) ;
11、使用 order by 子句对行进行排序
order by 子句用于对从数据库中检索出来的行进行排序。order by子句可以指定一行或者多列(查询结果会根据这些列对数据进行排序),
而且必须位于 from 或者 where 子句(如果提供了where子句)之后。
select * from customers order by last_name ;
默认情况下,order by子句会按照升序顺序对数据进行排序(较小的值先出现),
也可以使用desc关键字指定按照降序的顺序对数据进行排序(较大的值先出现)。
asc关键字显式地说明采用升序顺序进行排序。
select * from customers order by first_name asc , last_name desc ;
对从customers表中检索出来的行首先根据first_name进行升序排序,然后根据last_name进行降序排序。
在order句子中,也可以根据列的次序指定对哪一列进行排序:1表示第一列排序,2表示第二列排序,以此类推。。。。
select customer_id,first_name,last_name from customers order by 1 ;
上面的示例中,对第一列进行排序,1 代表第一列(customer_id)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值