oracle 基础SQL语句 多表查询 子查询 分页查询 合并查询 分组查询 group by having order by

select语句学习

. 创建表

create table user(user varchar2(20), id int);

. 查看执行某条命令花费的时间

set timing on;

. 查看表的结构

desc 表名;

. 查询所有列

select * from 表名;

. 查询指定列

select 某列名1,某列名2 from 表名;

. 取消重复行

select distinct 某列名1,某列名2 from 表名;

其中distinct作用在后面多列,只有每行完全相同才会被滤去

. 给某列或者某个表取别名

select 某列名 as 其他名 from 表名 as 其他名;

. 如何处理null值

nvl函数的使用:select nvl(某列名,0) from 表名

当此列为null时将值置为0

. 对时间类型的数据的处理

select 某列1,某列2 from 表名 where 列名=\'1-1月-1982\';

oracle默认的时间格式如上

like

%表示0到多个字符

_表示单个字符

select 某列名 from 表名 where 列名 like G%;

返回首字母为G的列

in

select 某列名 from 表名 where 列名 in(条件a,条件b,条件c);

等同于 select 某列名 from 表名 where 列名 = 条件a,列名 = 条件b,列名 = 条件c;

null的处理

select 某列名 from 表名 where 列名 is null;

不是用等号也不能将null写成\'\'

order by

select 某列名 from 表名 order by 列名 asc; 从低到高asc可省略

select 某列名 from 表名 order by 列名 desc;从高到低

select 某列名 from 表名 order by 列名1 asc,列名2 desc;其中列1和列2之间的逻辑要正确

select 某列名*2 as 别名 from 表名 order by 表名 asc; 使用别名排序达到一个很好的效果

max分组函数:在没有使用order by的时候select后要么全是分组函数,要么就是没有分组函数

select max(列名) from emp;

select 列名1 from 表名 where 列名2=(select max(列名2) from 表名);

select 列名1, max(列名2) from 表名;错误,

min avg sum count 使用类似

group by 和 having的使用

group by用于对查询的结果进行分组统计

having 用于限制分组显示的结果

select avg(列名),max(列名) ,列名x from 表名 group by 列名x;

select avg(列名),max(列名) ,列名x,列名y from 表名 group by 列名x,列名y;

先按列名x分组再按列名y分组

select avg(列名),max(列名) ,列名x from 表名 group by 列名x having avg(列名)>2000;

显示 >2000 的组

1 分组函数只能出现选择列表、having、order by子句中

2 如果在select语句中同时包含有group by ,having,order by那么他们的顺序是group by ,having,order

by

3 在选择列中如果有列、表达式、和分组函数,那么这些列和表达式必须有一个出现在group by子句中,否则会

出错

select 列名1,avg(列名2),max(列名3) from 表名 group by 列名1 having avg(列名2)<2000;

其中列名1就一定要出现在group by 中

多表查询

将表取个别名就行了

多张表多表查询

使用select时:

第一步:select ?,?,? from talbe1 a1,table2 a2 where a1.x between a2.x and a2.y;

第一步:select a1.x,a2.y,a1.z from talbe1 a1,table2 a2 where a1.x between a2.x and a2.y;

实现的功能是:显示表1的x、表2的y、表1的z,条件是表1的x在表2的x和y之间;

一张表进行“多表查询”(自连接)

将一张表取多个别名进行操作:

select ?,?,? from talbe1 a1,table1 a2 where a1.x between a2.x and a2.y;

数据库在执行每个子句sql是从左到右执行的,子句与子句先执行后面的。

子查询

1什么是子查询

子查询是指嵌入在其他sql语句中的select语句,也叫嵌套查询

2如何进行子查询?

子查询就是将条件不断分解,再将条件不断叠加

单行子查询:select ? from table where 条件=(select ...... select()......);

多行子查询:例1:select ? from table where 条件 in (select ......);

例2:select ? from table where 条件 >all (select ...... );

例2等同于:select * from table where 条件 >(select max()...... );

例3:select ? from table where 条件 >any (select ...... select()......);

例3等同于:select * from table where 条件 >(select min()...... );

多列子查询:select ? from table where (条件1,条件2) = (select a, b where ......);

在from子句中使用子查询:select ? from table a1, (select ? from talbe where..) a2 where ....;

再复杂点:select ? from table a1, (select x, avg(y) w from talbe group by x) a2 where ....; ---此时avg(y)必须有个别名

oracle给表取别名不能用as,报错;给列取别名可以用as。

oracle分页查询(一般用于打印报表时使用)(不同数据库语法不同)

第一步:rownum分页:

select a1.*, rownum rn from (select * from table) a1;

第一步:显示rownum:

select a1.* , rownum rn from (select * from table) a1 where rownum < 某行;

第三步:截取某段

select * from (select a1.* , rownum rn from (select * from table) a1 where rownum <

某行) where rn >某行;

在此基础上要改变查询的内容,所有的操作都是在最内层的括号里或者那两个行号操作。

用查询结果创建新表

create table table2 (列1,列2,列3) as select 列11,列22,列33 from table;

合并查询(oracle特有语法)(据说这些命令效率最高)

1)union 并集

select * from table where 条件1 union select * from table where 条件2;

(将两个select合并,并将重复的多余的去掉)

2)union all

用于同上,但是这个命令不去掉重复的内容

3)intersect 取交集

4)minus 取差集

oracle关于日期格式的问题:

在插入日期数据时默认采用的格式是:dd-mm月-yyyy

如果想用别的格式插入数据需要使用函数:to_date(\'1991-11-20\',\'yyyy-mm-dd\')