Oracle - 简单的 SELECT 的使用

来源:互联网 发布:软件编程技术 编辑:程序博客网 时间:2024/05/24 07:19
Oracle - SELECT 及过滤和排序

一、SELECT的基本使用
> 查询返回所有数据:select * from tablename;
> 查询返回一部分字段:select 字段1,字段2 from tablename;

> 列的别名一般用双引号,以便在别名中包含空格或特殊字符,别名的写法有两种方式:
    > 紧跟在字段(列)后面
       select 字段a a_name,字段b from tablename;
       select 字段a “a name”,字段b from tablename;
    > 在列名和别名之间加入关键字 ‘as’
       select 字段a as a_name,字段b from tablename;
    > 字符串用单引号 ' ' 来连接,只有在取别名时,才会用双引号 ""

> 连接符:把列与列连接在一起,用 '||' 表示,可以用来合成列
   select last_name || ' `s job_id is ' || job_id from employees;

> 去除重复:使用关键字 ‘distinct’
   select distinct 字段a from employees;  去除字段 a 重复的数据

> 显示表的结构:使用 desc[ribe] tablename;

二、在查询中过滤行
> 常用的比较运算符:等于(=)、不等于(!= 或 <>)、大于(>)、大于等于(>=)、小于(<)、小于等于(<=)
> 其他比较运算符:
    > 在两个值之间,包含边界(between ... and ...):
        > select * from tablename where id between 10 and 100;
    > 等于值列表中的一个( in(set) )
        > select * from tablename where id in(10, 20, 30);
    > 模糊查询(like),使用like运算选择类似的值,选择条件可以包含字符或数字:
        > % 代表零个或多个字符(任意个字符)
        > _  代表一个不确定的字符(比如he_lo,表示第三个字符是不固定的,其他是固定的)
        > select * from tablename where name like ' he_llo % ' ;
    > 空值(is null)
        > select * from tablename where id is null;  
        > select * from tablename where id is not null;
    > 回避特殊符号(escape) :使用转义符
        > select * from tablename where id like ' IT\_% ' escape ' \ ' ;

> 使用 where 子句,将不满足条件的行过滤
    > 带单条件的查询:select * from tablename where id>=10;
    > 带多条件的查询:select * from tablename where id>=10 and  id < 100; // and(且)、or(或)、否(not)
    > 查询时,字符和日期要包含在单引号 ( ' ' ) 中,日期格式敏感,默认的格式是:DD-MON月-RR

三、在查询中排序
> 使用 order by 排序,order by 子句在select语句的结尾
    > asc (ascend):升序                desc(descend):降序
    > select * from tablename order by id; // 默认对 id 字段升序排列
    > select * from tablename order by id desc; // 对 id 字段降序排列
0 0
原创粉丝点击