03【玩转Oracle】初识查询语句及其条件使用

这章节讲的主要是查询(select)语句的使用

不同终端的Oracle连接(已更)
table.sql脚本的执行(已更)

1.字段的比较
select * from 表名 where 表中的字段名 < 80 ;
select * from SELECTIVEINFO where GRADE <80 and rownum <= 5;

2.限制查询行数
select * from 表名 where rownum < 行数; (这里使用 = 会提示’不可行’,只能<=)
select * from SELECTIVEINFO where rownum < 5;

3.比较日期
select * from 表 where 日期字段名 > ‘yyyy-mm-dd’;
select * from SELECTIVEINFO where SELECTIVEDATE > '04-12月-10';
(后面的字符串格式要与日期字段的格式要一致,否则提示’无效xxx’)
如果不一致仍要比较,可以使用代码改格式,如下:(有错误,查询出来显示了全部行)
如select * from SELECTIVEINFO where to_char(SELECTIVEDATE,‘yy-mm-dd’) > ‘04-12-19’;

4.查询字段不等于’xx’的
select * from 表名 where 字段名 <> ‘xx’ ;
select * from 表名 where 字段名 not in ‘xx’ ;
select * from 表名 where 字段名 not like ‘xx’ ;
select * from SELECTIVEINFO where classid <> 1;
select * from STUDENTINFO where STUDENTNAME not in 'a';
select * from STUDENTINFO where STUDENTNAME not like 'a';

5.between… and…范围
select * from SELECTIVEINFO表名 where 字段名 between 下限值 and 上限值;
select * from SELECTIVEINFO where grade BETWEEN 60 and 75;

6.IN运算符
select * from 表名 where 字段名 in(字段值1,字段值2,字段值…);
select * from SELECTIVEINFO where classid in(1,3);
解释:在表SELECTIVEINFO中查询字段classid为1或者3的行。

作业:

东软公司覃老师提供
7.Like 运算符(模糊查询)
% 代表零或任意更多的字符
_ 代表一个字符

①查询姓名为h开头的学生
select * from STUDENTINFO where studentname like 'h%';

②查询学生名字只有一个字符的学生
select * from STUDENTINFO where studentname like '_';

③查询学生名字第二个字符为e的学生
select * from STUDENTINFO where studentname like '_e%';

④查询地址以”no_”开头的学生信息
select * from STUDENTINFO where address like 'no@_%' escape '@';

⑤查询fax为null的学生id和fax
select studentid,fax from STUDENTINFO where fax is null;

⑥查询倒数第二个字符为n的学生信息
select * from STUDENTINFO where studentname like '%n_';

作业:

东软公司覃老师提供
8. AND运算符
select * from表名 where 字段名 > 值 and 字段名 in(值1,值2);
select * from SELECTIVEINFO where grade > 85 and classid in(1,3);

9.OR运算符
select * from 表名where 字段名> 值 or 字段名 = 值 ;
select * from SELECTIVEINFO where grade > 85 or classid = 1;

10.NOT运算符
select * from 表名 where 字段名 is not 值;
select * from SELECTIVEINFO where classid is not 1;

11.逻辑运算符:也就是多重条件
反正就记住多加括号即可,不要自以为结果是自己想的那亚子 。

作业:

东软公司覃老师提供
12.ORDER BY语句
select * from SELECTIVEINFO where grade < 85 order by GRADE desc;
ASC升序(默认)
DESC降序(总是记不住这两个单词,就记住两个单词后面都是SC就行了 )
①ORDER BY 子句必须写在SELECT语句的最后
②空值null在升序排列中排在最后,在降序排列中排在最开始;
③ORDER BY 可以排序没有select中的字段
select NAME from SELECTIVEINFO where grade < 85 order by GRADE;
④可以多列排序
select NAME from SELECTIVEINFO where grade < 85 order by GRADE ,NUM_ID;
⑤可以以select字段的序号来排序
select NAME,GRADE ,NUM_ID from SELECTIVEINFO where grade < 85 order by 1 ,3;这里的13对应的是GRADENUM_ID

作业:

东软公司覃老师提供

1.select 员工姓名,部门编号 from 表 where 部门编号 in(20,30) order by 工资 ASC;
2.select 员工姓名,部门,编号,工资 from 表 where (工资 between 2000 and 3000) and 部门 not in(10) order by 部门 ,工资DESC。
3.select 员工姓名,入职日期,职位 from 表 where to_char(入职日期,'yy') = '82' and 职位 like in(SALES%,MAN%) order by 入职日期 DESC;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值