2018-1-6

1、select语句中,可以使用where关键字条件检索,where关键字写在表名的后面,如果whereorder 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=’男’)

7in运算符可以在指定的多个信息中进行匹配。

   格式:  in (‘1’,’2’,’3’,........)

   例:select  *  from  teacher  t  where  t.birplace  in  (‘北京’,’上海’)

8not 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、聚合分析

    1sum(字段)——按照指定字段求和

       例: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、countdistinct 字段)——排重处理(统计该字段不重复的有多少条记录)

例: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语句的查询条件

例:1select * from teacher t where t.dno=(select t1.dno from teacher t1 where t1.tname='李树标')

 

2select *

           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.字段

注意:当查询多张表时,需要写关联条件,否则会出现笛卡尔乘积

例:  1select t.tname,d.dname from teacher t,dept d where t.dno=d.dno

          2select 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数据库中,可以在关联语句上右侧表字段上加上(+)以实现左外关联

17union运算

    union —— 求两个相同字段类型和字段个数的sql语句的并集,且排重;而union all 则是取并集,且不排重

例:/*求学校中所有男教师和男学生的姓名*/

   select t.tname from teacher t where t.tsex=''

union

select s.sname from student s where s.sex=''

18intersect——求两个相同字段类型和字段个数的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='赵汗青'

/*求赵汗青和韩信选了哪些相同的课程*/

19minus——求两个相同的字段类型和字段个数的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 '%计算机%')

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值