Oracle基础-查询

查询

  • 基本查询语句
  • 在SQL*PLUS 中设置格式
  • 字段设置别名
  • 运算符和表达式
  • 模糊查询 - LIKE
  • 范围查询
  • 查询结果排序
  • case when语句
  • decode函数

基本查询语句

SELECT [DISTINCT] column_name1,… FROM table_name [WHERE conditions];

在SQL*PLUS 中设置格式

  • COLUMN column_name HEADING new_name;
    例子:COLUMN username HEADING 用户名;
  • COLUMN column_name FORMAT dataformat;
    例子:COLUMN username FORMAT a10;
    COLUMN username FORMAT 9999.9;
  • COLUMN column_name CLEAR;

字段设置别名

SELECT column_name AS new_name,… FROM table_name;

运算符和表达式

表达式是由操作数和运算符组成;操作数可以是变量、常量和字段。

算法运算符(+,-,*,/)
比较运算符(>,>=,<,<=,=,<>)
逻辑运算符(and,or,not)

模糊查询 - LIKE

通配符(_,%)
一个_只能代表一个字符
%可以代表0到多个任意字符
例子:查询用户名以a开头的用户信息
SELECT * from users where username like ‘a%’;
查询用户名第二个字符是a的用户信息
SELECT * from users where username like ‘_a%’;
查询用户名含有a的用户信息
SELECT * from users where username like ‘%a%’;

范围查询

  • AND

  • BETWEEN … AND
    闭合区间
    例子:
    salary在[800,2000]
    select * from users where salary between 800 and 2000;
    salary不在[800,2000]
    select * from users where salary not between 800 and 2000;

  • IN/NOT IN
    查询用户名是aaa或者bbb的用户信息
    select * from users where username in (‘aaa’,‘bbb’);

查询结果排序

SELECTFROM …[WHERE] ORDER BY column1 DESC/ASC,…;
DESC降序,ASC升序

case when语句

CASE column_name WHEN value1 THEN result1,… [ELSE result] END

例子:
select username,case username when ‘aaa’ then ‘计算机部门’ when ‘bbb’ then ‘市场部门’ else ‘其他部门’ end as 部门 from users;

CASE WHEN column_name1=value1 THEN result1,…[ELSE result] END

select username,case when username =‘aaa’ then ‘计算机部门’ when username=‘bbb’ then ‘市场部门’ else ‘其他部门’ end as 部门 from users;

decode函数

decode(column_name,value1,result1,…defaultvalue);
例子:
select username,decode(username,‘aaa’,‘计算机部门’,‘bbb’,‘市场部门’,‘其他’) from users;

你可能感兴趣的:(Oracle)