7.1. Overview
7.1. Overview
7.1.查询概览
The process of retrieving or the command to retrieve data from a database is called a
query
. In SQL
the SELECT command is used to specify queries. The general syntax of the
SELECT
command is
从数据库中检索数据的过程或命令称为查询。在SQL中,
SELECT命令用于指定查询。
SELECT命令的一般语法为:
[WITH with_queries] SELECT select_list FROM table_expression
[sort_specification]
The following sections describe the details of the select list, the table expression, and the sort specification.
WITH
queries are treated last since they are an advanced feature.
以下各节介绍了选择列表,表表达式和排序规范的详细信息。由于WITH查询是高级功能,因此将最后介绍它。
A simple kind of query has the form:
简单的查询格式如下:
SELECT * FROM table1;
Assuming that there is a table called
table1
, this command would retrieve all rows and all userdefined
columns from
table1
. (The method of retrieval depends on the client application. For example,
the psql program will display an ASCII-art table on the screen, while client libraries will offer
functions to extract individual values from the query result.) The select list specification
*
means all
columns that the table expression happens to provide. A select list can also select a subset of the available
columns or make calculations using the columns. For example, if
table1
has columns named
a
,
b
, and
c
(and perhaps others) you can make the following query:
假设有一个名为table1的表,此命令将从table1中检索所有行和所有用户定义的列。(检索方法取决于客户端应用程序。例如,psql程序将在屏幕上显示ASCII码表格,而客户端库将提供从查询结果中提取单个值的功能。)选择列表规范*表示提供表的所有列。选择列表还可以选择可用列的子集或使用这些列进行计算。例如,如果table1具有名为a,b和c(可能还有其他)的列,则可以进行以下查询:
SELECT a, b + c FROM table1;
(assuming that
b
and
c
are of a numerical data type). See Section 7.3 for more details.
(假设b和c为数字数据类型)。更多详情,请参见
7.3节
。
FROM table1
is a simple kind of table expression: it reads just one table. In general, table expressions
can be complex constructs of base tables, joins, and subqueries. But you can also omit the table
expression entirely and use the
SELECT
command as a calculator:
FROM table1是一种简单的表表达式:它仅读取一个表。通常,表表达式可以是基表,联接和子查询的复杂构造。但是,您也可以完全省略表表达式,并将SELECT命令用作计算器:
SELECT 3 * 4;
This is more useful if the expressions in the select list return varying results. For example, you could
call a function this way:
如果选择列表中的表达式返回不同的结果,则将更为有用。例如,可以通过以下方式调用函数:
SELECT random();