Oracle_学习使用SQL语句二

[b]一、like操作符[/b]

like操作符用于执行模糊查询,当执行查询操作时,如果不能完全确定某些信息的查询条件,但这些信息又具有某些特征,那么可以使用模糊查询,当执行模糊查询时,需要使用通配符"%"和"_",其中"%"(百分号)用于表示0个或多个字符,"_"(下划线)用于表示一个字符,如果要将[color=red]"%"、"_"、"'"、"&"[/color]做为查询条件时,那么需要通过escape对[color=red]"%"、"_"[/color]进行转义或通过ASCII编码进行转义,而"&"则通过ASCII编码或"||"将查询的字符进行拼接,"'"则通过ASCII编码或通过[color=red]''替换'[/color]进行转义,下面实例说明使用like操作符的方法:
[b] 1、查询首字符为"a"的所有员工姓名的信息:[/b]
select * from temps where name like "a%";

[b] 2、查询第三个字符为大写"A"的所有员工姓名的信息:[/b]
select * from temps where name like "__A%";

[b]二、Oracle转义符[/b]
[b]1、对"&"进行转义,update、insert、select操作均需要进行转移,但是不能通过转义字符查找,可以通过ASCII编码或"||"将查询字符拼接进行查询,例如:[/b]

update temps set name='myjsp?page=1'|| '&' ||'page=10' where id=27;
update temps set name='index.do?page=1' || chr(38) || 'page=10' where id=88;
select * from temps where name like '%'||chr(38)||'%';
select *from temps where name like '%.do%' ||chr(38)|| '%';

[b]2、对"-"进行转义,insert、update操作可以不需要转义,但是select操作需要通过escape或通过ASCII进行转义,例如:[/b]

update temps set name='index.do_1' where id=28;
select * from temps where name like 'a=_%' escape '=';
select * from temps where name like 'a__%' escape '_';
select * from temps where name like '__/_%' escape '/';
select * from temps where name like '%\_%' escape '\';
----查询带有--字符并且以ab开始的记录的记录
select * from temps where name like 'ab=_=_%' escape '=';
select * from temps where name like '%\_\_%' escape '\';

[b]3、对"%"进行转义,insert、update操作可以不需要转义,但是select操作需要通过escape或通过ASCII进行转义,例如:[/b]

update temps set name='index.do%' where id=100;
--查询带有%字符的记录
select * from temps where name like '%\%%' escape '\';
select * from cip_temps where name like 'index.do=%%' escape '=';
---查询带有%%字符的记录
select * from temps where name like 'index.do\%\%%' escape '\';
select * from temps where name like '%/%/%%' escape '/';
select * from temps where name like '%=%=%%' escape '=';
select * from temps where name like '%'%';

[b]3、对"'"进行转义,insert、update操作可以不需要转义,但是select操作需要通过 [color=red]''替换'[/color]或通过ASCII进行转义,例如:[/b]
--用两个''代替'。
update temps set name='index''99' where id=67;
--用ascii码
select * from cip_temps where name like '%'||chr(39)||'%';
[b]三、in操作符[/b]
IN操作符用于执行列表匹配操作,当列或表达式结果匹配于IN列表中的任意一个值时,则返回true。例如:
select * from temps where name in('aa','bb','cc');

[b]四、is null操作符[/b]
is null 操作符用于检测列或表达式结果是否为null,如果结果为null则返回true,否则返回false,例如:
select * from temps where name is null;

当于null进行进行比较时,不能使用"="、'<>'等逻辑操作符进行比较,尽管使用他们进行比较不会报错,但是条件子句返回总是false。例如:

select * from temps where name = null;
结果是没有查询出记录。

[b]五、order by子句[/b]
在执行查询操作时,经常对查询的结果进行排序,以显示更直观的数据,数据排序是使用order by子句来实现的,其语法是:

SELECT "栏位名" FROM "表格名" [WHERE "条件"]ORDER BY "栏位名" [ASC, DESC]

[color=red]主意:当select语句中同是包含多个子句(where、group by、having、order by等)时,order by必须是最后一条子句。[/color]
[b]1、升序排序[/b]
默认情况下,当使用order by执行排序操作时,数据以升序方式排序,也可以在排序列后制定ASC关键字。例如:
select * form order by id[asc]

[color=red]
注意:当执行升序排序时,如果排序列包含null值,那么null会显示在最后面,Oracle在Order by 时缺省认为null是最大值。
[/color]
[b] 2、降序排序[/b]
当使用order by 子句进行排序操作时,如果进行降序排序操作,则必须制定desc关键字。例如:
select * form order by id desc

[color=red]
注意:当执行降序排序时,如果排序列包含null值,那么null会显示在最前面,Oracle在Order by 时缺省认为null是最大值
[/color]
[b]3、使用多列排序[/b]
当使用order by子句进行排序操作时,不仅可以基于单列或单个表达式进行排序,也可以基于多个列或多个表达式进行排序,当基于多个列或多个表达式进行排序操作时,首先按照第一列或表达式进行排序,当第一列或表达式存在相同数据时,然后以第二列或表达式进行排序操作,以此类推.....,例如:
select *from temps order by age asc,name desc;

[b]4、使用非选择列进行排序[/b]
可以使用没有必须显示的列或表达式进行排序,例如:
select age from temps order by name desc;

[b]5、使用列别名进行排序[/b]
如果为列或表达式定义了别名,那么进行排序操作时,可以通过别名进行排序,例如:

select name as "姓名" from cip_temps order by "姓名";

[b]6、根据列位置编号进行排序[/b]
可以按照列或表达式在选择列表中的位置进行排序,例如:
select name,age from temps order by 2 desc;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值