Oracle database query of a simple summary

Fourth, a simple query

The main feature is the simple query all the data rows of a data table in the display, and then may be utilized to control the output of the SELECT clause column required.

4.1, basic grammar

Example : emp table query data (all data queries)

SELECT * FROM emp;

After obtaining all the data can be found null information will be displayed on some columns, there is no representation of the contents of null, but null! = 0, null refers to the temporary unknown content.

  • Simple query syntax:
SELECT [DISTINCT] * | 列名称[AS][列别名],列名称[AS][列别名],...
FROM 表名称[表别名];

Throughout the simple query in, there are two complete sentences:

SELECT clause: In the presence of the following clauses which:

*: Indicates that the query all data columns;

Column Name: Indicates the designated column, the column can also set an alias;

DISTINCT: removing the display indicates duplicate data.

FROM clause: the data table definitions to be used, it can be understood as a data source.

Examples : query each employee number, name, three basic wage information browsing.

SELECT empno, ename, sal
FORM emp;

Question on the implementation of the order, the order of execution of these two clauses:

The first step: the implementation of the FROM clause, indicating the identified data sources

The second step: the implementation of the SELECT clause to determine the data to be displayed column

4.2 Other operations inquiries

Examples : To find jobs now in the company of all employees

SELECT job FROM emp;       # 职位会有重复

SELECT DISTINCT job FROM emp;     # 去除重复数据

Although the use of DISTINCT eliminates duplicate all the data, but only limited to the contents of all columns all the same, if a empno field more than the increase in the above query,

SELECT DISTINCT empno,job FROM emp; 

You will find all the data have shown, because each number corresponding to the position is unique and different, so DISTINCT only be used for the entire contents of the same.

In addition to the above basic queries in simple query SELECT clause, which also supports four operations.

Examples : a database query so required by employee number, employee name, and years of base salary, basic salary day, year-end bonuses as standard

SELECT empno,ename,sal*12,sal/30 
FROM emp;

* 12 = monthly salary, salary = daily rate / 30

The results appear in decimals, the processing for the fractional part, leaving behind the one-way function is completed.

Examples : Now the company every employee can receive year-end awards at the end of 5000, requests for access to employee number, in base salary after the employee names and growth (excluding commissions)

SELECT empno,ename,sal*12+5000
FROM emp;

Examples : company employees every month an additional 200 yuan subsidy, this time queries employee number for each employee's name, in base salary

SELECT empno,ename,(sal+200)*12+5000
FROM emp;

The results appear in the table (sal + 200) * 12 + 5000 is ambiguous, you can play an alias salary

SELECT empno,ename,(sal+200)*12+5000 年薪
FROM emp;

When performing the alias, the AS may also be used a setting operation.

SELECT empno,ename,(sal+200)*12+5000 AS 年薪
FROM emp;

AS plus here if there is no difference to the final result, but must remind the reader that, if the alias settings, if in the program is certainly useless, then use the display is not large, but also to remember live, do not use the Chinese now are new, can use Chinese, so in the future when the program learned that we should avoid the Chinese!

Examples : set an alias for the query results

SELECT empno AS 雇员编号,ename AS 雇员姓名,(sal+200)*12+5000 AS 年薪
FROM emp;

At this time there is a new problem, given the salary is what kind of salary? So I hope you can output an identification, direct output of one yuan logo below.

SELECT empno AS 雇员编号,ename AS 雇员姓名,(sal+200)*12+5000 AS 年薪,'¥' AS 货币
FROM emp;

The definition of query results in the currency of identity is actually a string constant, constant use direct output mode.

Example : using "||" connected display

When performing a simple query, may be used as "||" string or connection operation column data.

The first observation of the connection:

SELECT empno || ename FROM emp;

The results show that employee number and the names together.

SELECT '编号是:' || empno || ' 的雇员信息是:' || ename || '基本工资是:' || sal 雇员信息
FROM emp;

"Employee information" is the title name of the entire table settings.

Guess you like

Origin www.linuxidc.com/Linux/2019-09/160468.htm