1、在select语句中,可以使用where关键字条件检索,where关键字写在表名的后面,如果where和order by 同时出现,那么order by 写在where子句后面。
例:select * from teacher t where t.tsex=’男’ order by t.sal desc;
select * from teacher t where t.tage>’45’;
2、不等于——使用<>或者!=都能表示不等于
另外,数值可以不加’’
例:select * from teacher t where t.tage != 48;
select * from teacher t where t.tage <> 48;
3、between and 用于判断条件介于多少和多少之间
例:select * from teacher t where t.tage between 35 and 47;
4、对于空值的判断需要使用is null 或者is not null,而不能使用 = 或者 !=
例:select * from teacher t where t.tage is not null;
5、对于多个查询条件可以使用and进行连接表示并且,可以使用or进行连接表示或者
例:select * from teacher t where t.tage > 40 and t.tsex = ’男’;
select * from teacher t where t.tage > 40 or t.tage < ’25’;
6、当碰到运算符优先级的问题时,只需要使用()将你认为优先级高的表达式括起来即可
例:select * from teacher t where t.tsex=’女’ or (t.sal>9000 and t.tsex=’男’)
7、in运算符可以在指定的多个信息中进行匹配。
格式: in (‘值1’,’值2’,’值3’,........)
例:select * from teacher t where t.birplace in (‘北京’,’上海’)
8、not in 排除指定范围
格式: not in (‘值1’,’值2’,’值3’,........)
9、模糊查询
like ‘%值%’——全匹配(查询含有关键值的内容)
like ‘值%’——左匹配(查询以关键值开头的内容)
like ‘%值’——右匹配(查询以关键值结尾的内容)
例:select * from course c where c.cname like '%基础%'
select * from course c where c.cname like 'java%'
10、聚合分析
1、sum(字段)——按照指定字段求和
例:select sum(t.sal) sumsal from teacher t where t.tsex='男'
2、count(字段或*或1)——统计一共有多少条记录
例:select count(sno) from student s
3、max(字段)——统计该字段的最大值
4、min(字段)——统计该字段的最小值
例:select max(t.tage) from teacher t
select min(t.tage) from teacher t
5、avg(字段)——统计该字段的平均值
例:select avg(t.sal) from teacher t
6、count(distinct 字段)——排重处理(统计该字段不重复的有多少条记录)
例:select count(distinct sal) from teacher t
11、分组
使用group by 可以进行分组
group by 语句写在where子句的后面(如果还有order by ,则order by 写在最后)
group by 语句可以跟若干个字段,表示按照这些字段进行分组
group by 语句后的字段必须包含select后的所有非函数字段
例:select t.dno,max(t.sal) from teacher t group by t.dno
如果需要在组的层面上增加过滤条件,则需要使用having条件,而不能使用where,因为where条件指定针对字段
注意:having语句必须写在group by 语句后面(order by还是位于最后)
例:select t.birplace,count(*) from teacher t group by t.birplace having count(*)>1
12、子查询
定义:第一条sql语句其结果作为第二条sql语句的查询条件
例:1、select * from teacher t where t.dno=(select t1.dno from teacher t1 where t1.tname='李树标')
2、select *
from student s
where s.birthday <
(select s1.birthday from student s1 where s1.sname = '赵静')
and s.dno = (select s2.dno from student s2 where s2.sname = '赵静')
/*查询系别和赵静相同但是年龄比她大的同学信息*/
13、多行子查询
当我们的子查询为多行结果时,外层的父查询需要使用in来关联到子查询,而不能使用=
14、可以在select语句结尾处使用for update来方便的在图形化界面上进行数据的增删改操作
例:select * from teacher t for update
或者 select t.*,t.rowid from teacher t
15、在一条sql语句中,可以在from后面跟上多张表,用来检索多张表中的信息。
常用格式:select 字段 from 表1,表2 where 表1.字段=表2.字段
注意:当查询多张表时,需要写关联条件,否则会出现笛卡尔乘积
例: 1、select t.tname,d.dname from teacher t,dept d where t.dno=d.dno
2、select s.sname,c.cname,t.tname from student s,course c,teacher t,s_course sc,t_course tc
where
s.sno=sc.sno and sc.cno = tc.cno and c.cno=tc.cno and tc.tno =t.tno
16、左外关联
左边的一张表中的记录一定会展示,而右边的一张表中的记录如果有,就展示,如果没有,就留空
格式:select 字段 from 左表 left join 右表 on 左表.字段=右表.字段
例:select t.tname,d.dname from teacher t left join dept d on t.dno = d.dno
注意:在oracle数据库中,可以在关联语句上右侧表字段上加上(+)以实现左外关联
17、union运算
union —— 求两个相同字段类型和字段个数的sql语句的并集,且排重;而union all 则是取并集,且不排重
例:/*求学校中所有男教师和男学生的姓名*/
select t.tname from teacher t where t.tsex='男'
union
select s.sname from student s where s.sex='男'
18、intersect——求两个相同字段类型和字段个数的sql语句的交集
例:select c.cname from student s,s_course sc,course c
where s.sno=sc.sno and sc.cno=c.cno and s.sname='韩信'
intersect
select c1.cname from student s1,s_course sc1,course c1
where s1.sno=sc1.sno and sc1.cno=c1.cno and s1.sname='赵汗青'
/*求赵汗青和韩信选了哪些相同的课程*/
19、minus——求两个相同的字段类型和字段个数的sql语句的差集
例:/*求哪些课程被赵汗青所选而没有被韩信所选的课程*/
select c1.cname from student s1,s_course sc1,course c1
where s1.sno=sc1.sno and sc1.cno=c1.cno and s1.sname='赵汗青'
minus
select c.cname from student s,s_course sc,course c
where s.sno=sc.sno and sc.cno=c.cno and s.sname='韩信'
20、多表子查询
例:/*使用子查询求计算机系的老师编号和姓名*/
select t.tno,t.tname from teacher t where t.dno=(
select dno from dept where dname like '%计算机%')