这章节讲的主要是查询(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;
这里的1,3对应的是GRADE ,NUM_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;