oracle10g sql查询基础笔记

来源:互联网 发布:网络小电视浴室电视机 编辑:程序博客网 时间:2024/05/07 04:45

1.常识基础
  
   聚集函数不能出现在WHERE条件里面。
   分组的时候group by 后面出现的字段可以不在SELECT后面出现,但select里面出现的表字段必须在group by后面出现。
   一般使用聚集函数一定要想到用having,聚集函数一般是配合having来使用的。having要出现在group by之后。

 

2.模糊查询

select * from aa where a1 like 'a%'; 以a开头任意个字符结尾的。'a_'以a开头一个任意字符结尾的,如果有多个可以用多个'a__'。
select * from aa where a1 like '%a%';只要a1字段包含了a的都查询出来。

 

3.表连接


 内连接
 外连接:左外连接、右外连接。

 

4.子查询

 

 无关子查询:in关键字 -->select * from e where id in (select id from d);

      exists关键字-> select * from e where exists (select id from d);判断子句select id from d;中是否有结果集,如果有数据则e中数据全部显示出来,否则不

显示。

 相关子查询:in关键字->select * from e where id in (select id from d where id=e.id and id='03');

      not in用法--> 取反:select * from e where id not in (select id from d where id=e.id and id='03');
       子查询的语句中不能有*。

      注意:exists字句中可以有*符号。
      exists用法->  select * from e where exists (select id from d where id=e.id);

      not exists用法->select * from e where not exists (select id from d where id=e.id);
   
             union用法-->表之间合并行数据,只是在显示一起没有物理的合并在一起:select eid,ename from e union select id,name from d;
      注意:它会去掉重复的数据。
  
      intersect用法-->显示出两个表都匹配的数据行:selelct id from e intersect select id from d;
  
      批量插入数据方法:insert into e (eid,ename) select id,name from d;

      创建表参考别的表并且把数据拷贝过来,也可以用*指定字段也可以带条件:create table ttt as (select * from e);

原创粉丝点击