Study Notes: oracle study three: to retrieve the data base of the SQL language: simple query, filter query


This series is used as study notes, used to record the learning process, learning to deepen the impression, and revisit their own learning content only, reference books as "oracle 11g from entry to the master (Second Edition)" If further study, please purchase original books, thank you!

1, to retrieve data

User operation is the retrieval of data for most of the table or view, the data retrieval can be realized by the select statement. Syntax is as follows


select {[ distinct|all ] columns | *}
[into table_name]
from {tables | views | other select}
[where conditions]
[group by colunms]
[having conditions]
[order by columns]

It contains seven clauses in the above syntax:

  • select clause: means for selecting the data table, columns attempt;
  • into clause: a table structure and the original data into the new table;
  • from clause: specifies the data sources, including tables, views, and other select statement.
  • where clause: means for retrieving data screened
  • group by clause: means for grouping the search result display
  • having clause: data lines for screening from the demerits query group by using the packet itself in
  • order by clause: used to sort the result set (desc ascending, descending default)

1.1 simple query

1.1.1 retrieve all columns

And contains only select from clause. select clause is used to select the columns you want to appear in the query results, you can also use * to represent retrieve all columns.

Example 1: select * All data in the retrieval table dept mode scott

select * from dept;

Note: If the above select statement running in system mode, is required scott.emp.

Example 2: In scott mode, two data tables dept specified in the from clause, and salgrade

select * from dept,salgrade;

1.1.2 retrieve the specified column

Users can select after the keyword with specific column names to query the specified column, column names separated by commas directly, syntax is as follows:

select colunm_name1,column_name2,column_name1 from table_name;

Example: In scott mode, retrieving a column specified in the table emp job, ename, empno, as follows:

在Oracle数据库中,又要给标识行中唯一特性的行标识符,该行标识符的名称为ROWID,表示该行数据在数据库中的物理地址,属于隐藏列,并不定义在表中,所以也成为伪列。

select rowid,job,ename from emp;

1.1.3 查询日期列

日期列是指数据类型为date的列,查询日期列与查询其他列并无区别,但日期列的默认显示格式为DD-MON-YY.

例1:以简体中文显示日期结果


alter session set nls_date_language = 'SIMPLIFIED CHINESE';
select ename,hiredate from emp;

例2:以US-EN显示日期查询结果


alter session set nls_date_language = 'AMERICAN';
select ename,hiredate from emp;

例1:以特定格式显示日期查询记过


alter session set nls_date_format = 'YYYY"年"MM"月"DD"日"';
select ename,hiredate from emp;

例4:使用TO_CHAR函数定制日期显示函数

具体见to_char函数

1.1.4 带有表达式的select语句

使用select语句时,对于数字数据和日期数据都可以使用算数表达式,算数运算符包括:加+、减-、乘*、除/

select sal*(1+10) from emp;

1.1.5 为列指定别名

在Oracle系统中,为列指定别名既可以使用as关键字,也可以直接指定

例1:使用as指定列别名

select empno as "员工编号",ename as "员工名称",job as "职务" from emp;

例2:不使用as指定列别名

select empno "员工编号",ename "员工名称",job "职务" from emp;

1.1.6 显示不重复记录 distinct

在select语句中,可以使用distinct关键字限制在查询结果显示不重复的数据。

select distinct job from emp;

1.1.7 处理NULL值

NULL表示未知值,既不是空格,也不是0。在实际使用中可以使用nal处理NULL,将其转换成合理的显示结果。

例1:显示emp表中的雇员名、工资、奖金以及实发工资,但不处理NULL

select ename,sal,comm,sal+comm from emp;

例2:显示emp表中的雇员名、工资、奖金以及实发工资(SAL+COMM),使用nal处理NULL值

select ename,sal,comm,sal+nal(comm,0) from emp;

1.1.8 连接字符串

当执行查询操作时,为了让显示更有意义,通常需要将多个字符串连接起来,可以使用“||”或者concat函数。

例1:使用"||"操作符链接字符串

select ename,|| ""||'s job is '||job from emp;

例2:使用concat函数链接雇员名和工资信息

select concat(concatK(ename,'''s salary is'),sal) from emp;

1.2、筛选查询

在select语句中可以通过where语句实现对数据行筛选,筛选出符合条件的数据行。

语法格式如下:

select columns_list from table_name where conditional_expression

  • colunms_list:字段列表。
  • table_name:表名。
  • conditional_expression:筛选条件表达式

1.2.1 比较筛选

在where语句中可以通过比较运算符来筛选数据。

  • A=B: 比较A与B是否相等。
  • A!B: 或者A<>B,比较A与B是否不相等。
  • A>B: 比较A是否大于B.
  • A<B: 比较A是否小于B。
  • A>=B: 比较A是否大于等于B。
  • A<=B: 比较A是否小于等于B。

例1:在scott模式下,查询emp工资表中sal大于1500的数据记录

select empno,ename,sal from emp where sal > 1500;

另外,除了基本的”比较筛选“操作外,还有以下两个特殊的”比较筛选“操作/

  • A{operator}ANY(B): 表示A与B中的任何一个元素进行opertor运算符的比较,只要有一个比较值为TURE,就返回数据行
  • A={operator}ALL(B): 表示A与B中的所有元素进行opertor运算符的比较,只有与所有元素比较直为徒惹,才返回数据行

select empno,ename,sal from emp where sal <> all(3000,950,800);

1.2.2 使用特殊关键字筛选

1.2.2.1 like关键字

like关键字可以使用以下两个通配符:

  • %:代表0个或者多个字符
  • _: 代表一个且只能是一个字符。

例1:在emp表中,使用like关键字匹配以字母S开头的任意长度的员工名称

select empno,ename,job from emp where ename like 'S%';

例2: 在emp表中,使用_匹配,员工职位第一个字符是S,第三个字符是L,第5个字符是S的值

select empno,ename,job from emp where job like 'S_L_S%';

注意:可以在like、in、between、is null关键字前面加上NOT,表示否定的判断

如果要查询的字符串含有%、——等字符时,可以使用转义字符实现查询,如下所示


create table dept_temp as select * from dept; --新建表dept_temp表结构和dept一致
insert into dept_temp values('60','IT_RESEARCH','BEIJING');
select * from dept_temp where dname like 'IT\_%' escape '\';

1.2.2.2 IN关键字

格式:IN(目标值1,目标值2。。。 目标值n)

例子1:

select empno,ename,job from emp where job in ('PRESIDENT','MANAGER','ANALYST');

例子2:

select empno,ename,job from emp where job in ('PRESIDENT','MANAGER','ANALYST');

1.2.2.3 between关键字

用于需要返回一个值是否位于范围之间,通常使用between。。。and...和not between。。。and。。。来指定范围条件。

例子1:在emp表中,使用between...and...关键字查询工资在2000到3000之间的员工信息

select empno,ename,sal from emp where sal between 2000 and 3000;

例子2:在emp表中,使用not between...and...关键字查询工资不在2000到3000之间的员工信息

select empno,ename,sal from emp where sal between 2000 and 3000;

1.2.2.4 IS NULL关键字

使用IS NULL来检测是否为空值。

1.2.3 逻辑筛选

逻辑筛选是指在where子句中使用AND\OR\NOT进行数据筛选操作

AND逻辑与,可以代替between...and...

例子1:

select empno,ename,sal from emp where sal >= 2000 and sal <= 3000;

OR逻辑运算符表示逻辑或

例子2:

select empno,ename,sal from emp where sal < 2000 or sal > 3000;

NOT运算符表示对表达式执行逻辑非运算。

Guess you like

Origin www.cnblogs.com/yj411511/p/11933047.html