查询设计

查询设计视图允许您创建和编辑数据库查询。

要访问此命令...

In a database file window, click the Queries icon, then choose Edit - Edit.


批注图标

大部分数据库都使用查询来筛选或排序数据库表格,以在您的计算机上显示记录。视图提供与查询相同的功能,但在服务器端提供。如果您的数据库处于支持视图的服务器上,可以使用视图来筛选服务器上的记录以加速显示。


批注图标

从数据库文档的选项卡页面中选择创建视图命令,您可以看到视图设计窗口,它与这里介绍的查询设计窗口相似。


“查询设计”窗口布局可以用已创建的查询存储,不能用已创建的视图存储。

设计视图

为创建查询,请单击数据库文档中的查询图标,然后单击在设计视图中创建查询

The lower pane of the Design View is where you define the query. To define a query, specify the database field names to include and the criteria for displaying the fields. To rearrange the columns in the lower pane of the Design View, drag a column header to a new location, or select the column and press +arrow key.

在“查询设计”窗口中的上方,显示了查询设计栏和设计栏的图标

如果希望测试查询,请在数据库文档中双击该查询名称。查询结果显示在类似于“数据源视图”的表格中。注:显示的表格只是临时的。

查询设计视图中的键

功能

F4 键

预览

F5 键

运行查询

F7 键

添加表格或查询


浏览

When you open the query design for the first time, you see a dialog in which you must first select the table or query that will be the basis for your new query.

双击字段可以将其加入到查询。拖放可以定义关系。

批注图标

在设计查询期间,无法修改选定的表格。


删除表格

要把一个表格从设计视图中移除,请点击表格窗口的上部边框,调出上下文菜单。使用命令删除把表格从设计视图中移除。另外,您也可以使用删除键来移除。

移动表格并修改表格大小

可以根据需要来排列表格并改变其大小。要移动一个表格,拖动其上边框到希望放置的位置。要放大或缩小一个表格的显示比例,则拖动其边框或一个角,直到得到想要的大小。

表格关系

如果一个表格中的一个数据字段与另一个表格中的一个数据字段之间存在数据关系,那么您可以把此关系用于您的查询。

If, for example, you have a spreadsheet for articles identified by an article number, and a spreadsheet for customers in which you record all articles that a customer orders using the corresponding article numbers, then there is a relationship between the two "article number" data fields. If you now want to create a query that returns all articles that a customer has ordered, you must retrieve data from two spreadsheets. To do this, you must inform LibreOffice about the relationship which exists between the data in the two spreadsheets.

To do this, click a field name in a table (for example, the field name "Item-Number" from the Customer table), hold down the mouse button and then drag the field name to the field name of the other table ("Item-Number" from the Item table). When you release the mouse button, a line connecting the two fields between the two table windows appears. The corresponding condition that the content of the two field names must be identical is entered in the resulting SQL query.

只有使用 LibreOffice 作为关系数据库的界面,才能建立以多个关联工作表为基础的查询。

批注图标

不能通过同一个查询访问多个数据库中的表格。只能在一个数据库中建立涉及多个表格的查询。


Specifying the relation type

If you double-click on the line connecting two linked fields or call the menu command Insert - New Relation, you can specify the type of relation in the Relations dialog.

也可以按住 Tab 键直到选中线条,然后按 Shift+F10 组合键来显示上下文菜单并选择指令编辑。某些数据库只支持可能联接类型的子集。

删除关系

要删除两个表格间的关系,请用鼠标点击其连线并随后按删除键。

另外,也可以在关系对话框中删除相关字段包含的相应条目,或者按 Tab 键找到突出显示的连结向量,然后按 Shift+F10 键打开上下文菜单并选择删除命令。

Defining the query

选择条件以定义查询。设计表格的每列都接受用于查询的数据字段。一行中的条件用布尔 AND 连接。

Specifying field names

First, select all field names from the tables that you want to add to the query. You can do this either by drag-and-drop or by double-clicking a field name in the table window. With the drag-and-drop method, use the mouse to drag a field name from the table window into the lower area of the query design window. As you do this, you can decide which column in the query design window will receive the selected field. A field name can also be selected by double-clicking. It will then be added to the next free column in the query design window.

删除字段名称

要将一个字段名称从查询中删除,请用鼠标单击该字段的标题,然后在该列的上下文菜单中启动命令删除

Saving the query

Use the Save icon on the Standard toolbar to save the query. You will see a dialog that asks you to enter a name for the query. If the database supports schemas, you can also enter a schema name.

略图

请输入为查询/表格视图指定的架构名称。

查询/表格视图名称

输入查询/表视图的名称。

筛选数据

To filter data for the query, set the desired criteria in the lower area of the query design window. The following options are available:

字段

Enter the name of the data field that is referred to in the Query. All settings made in the filter option rows refer to this field. If you activate a cell here with a mouse click you'll see an arrow button, which enables you to select a field. The "Table name.*" option selects all data fields with the effect that the specified criteria will be applied to all table fields.

别名

Specifies an alias. This alias will be listed in the query instead of the field name. This makes it possible to use user-defined column labels. For example, if the data field is named PtNo and, instead of that name, you would like to have PartNum appear in the query, enter PartNum as the alias.

In a SQL statement, aliases are defined as follows:

SELECT column AS alias FROM table.

例如:

SELECT "PtNo" AS "PartNum" FROM "Parts"

表格

The corresponding database table of the selected data field is listed here. If you activate this cell with a mouse click, an arrow will appear which enables you to select a different table for the current query.

排序

If you click on this cell, you can choose a sort option: ascending, descending and unsorted. Text fields will be sorted alphabetically and numerical fields numerically. For most databases, administrators can set the sorting options at the database level.

可见

If you mark the Visible property for a data field, that field will be visibly displayed in the resulting query. If you are only using a data field to formulate a condition or make a calculation, you do not necessarily need to display it.

条件

Specifies a first criteria by which the content of the data field is to be filtered.

或者

Here you can enter one additional filter criterion for each line. Multiple criteria in a single column will be interpreted as boolean OR.

You can also use the context menu of the line headers in the lower area of the query design window to insert a filter based on a function:

函数

The functions which are available here depend on those provided by the database engine.

If you are working with the embedded HSQL database, the list box in the Function row offers you the following options:

选项

SQL

效果

无函数

无函数可执行。

平均值

AVG

计算一个字段的算术平均值。

记数

COUNT

Determines the number of records in the table. Empty fields can either be counted (a) or excluded (b).

a) COUNT(*):如果使用一个小星号作为参数,那么该表的全部记录都将计算在内。

b) COUNT(column): Passing a field name as an argument counts only those records for fields in which the field name in question contains a value. Records with fields having Null values (i.e. fields containing no characters) will not be counted.

最大值

MAX

Determines the highest value of a record for that field.

最小值

MIN

Determines the lowest value of a record for that field.

总计

SUM

Calculates the sum of the values of records for the associated fields.

组合

GROUP BY

Groups query data according to the selected field name. Functions are executed according to the specified groups. In SQL, this option corresponds to the GROUP BY clause. If a criterion is added, this entry appears in the SQL HAVING sub-clause.


也可以直接在 SQL 语句中输入函数,其语法是:

SELECT FUNCTION(column) FROM table.

例如在 SQL 中调用计算总和的函数:

SELECT SUM("price") FROM "article".

Except for the Group function, the above functions are called Aggregate functions. These are functions that calculate data to create summaries from the results. Additional functions that are not listed in the list box might be also possible. These depend on the specific database engine in use and on the current functionality provided by the Base driver used to connect to that database engine.

To use other functions not listed in the list box, you must enter them manually under Field.

You can also assign aliases to function calls. If you do not want to display the query string in the column header, enter a desired substitute name under Alias.

SQL 语句中的相应函数是:

SELECT FUNCTION() AS alias FROM table

示例:

SELECT COUNT(*) AS amount FROM "article"

批注图标

If you run such a function, you cannot insert any additional columns for the query other than as an argument in a "Group" function.


示例

在以下示例中,通过两个表格来进行查询:"Item" 表格带有字段 "Item_No","Suppliers" 表格带有字段 "Supplier_Name"。另外,这两个表格还有一个共同的数据字段 "Supplier_No"。

要建立一个包括所有提供多于 3 种货物的供货商的查询,必须执行下列步骤:

  1. 把表 "article" 和 表"supplier" 插入查询设计。

  2. 假如此类型没有关系,那么链接两个表格的 "Supplier_No" 字段。

  3. Double-click on the "Item_No" field from the "Item" table. Display the Function line using the context menu and select the Count function.

  4. 输入条件 >3,然后关闭可见的字段。

  5. 在表格 "Suppliers" 中双击字段 "Supplier_Name",然后选择组合函数。

  6. 运行查询。

如果在表格 "Item" 中有字段 "preice"(货物的单价)和字段 "Supplier_No"(货物的供应商),那么您就能用以下查询得到供应商提供的货物的平均价格:

  1. 将 "Item" 表格插入查询设计。

  2. 双击 "Preice" 和 "Supplier_No" 字段。

  3. 打开函数行,然后在 "price" 字段中选择平均值函数。

  4. 你也可以在用于别名的行中输入“平均值”(不带引号)。

  5. 在 "Supplier_No" 字段选择组合。

  6. 运行查询。

下面是现有的上下文菜单命令和各式图标:

函数

Shows or hides a row for the selection of functions.

表格名称

显示或隐藏用于表格名称的行。

别名

显示或隐藏别名所在的行。

明确的数值

Retrieves only distinct values from the query. This applies to multiple records that might contain several repeating occurrences of data in the selected fields. If the Distinct Values command is active, you should only see one record in the query (DISTINCT). Otherwise, you will see all records corresponding to the query criteria (ALL).

例如,如果地址数据库中含有多个 "Smith",您可以选择明确的数值命令来指定 "Smith" 在查询中仅出现一次。

如果查询涉及多个字段,则所有字段的数值组合必须唯一的,以便从特定的记录中获得结果。例如,通讯簿中包括一条 "Smith in Chicago" 和两条 "Smith in London"。使用明确的数值命令查询 "last name" 和 "city" 两个字段后,查询结果中将返回一条 "Smith in Chicago" 和一条 "Smith in London"。

在 SQL 中,此命令对应于 DISTINCT 谓词。

限度

Allows you to limit the maximum number of records returned by a query.

If a Limit construction is added, you will get at most as many rows as the number you specify. Otherwise, you will see all records corresponding to the query criteria.

指定筛选条件

When formulating filter conditions, various operators and commands are available to you. Apart from the relational operators, there are SQL-specific commands that query the content of database fields. If you use these commands in the LibreOffice syntax, LibreOffice automatically converts these into the corresponding SQL syntax via an internal parser. You can also enter the SQL command directly and bypass the internal parser. The following tables give an overview of the operators and commands:

运算符

含义

如果...,则条件满足

=

等于

...该字段内容与指定的表达式一致。

The operator = will not be displayed in the query fields. If you enter a value without any operator, the = operator is automatically assumed.

<>

不等于

...该字段内容与指定的表达式不一致。

>

大于

...字段内容长于指定的表达式。

<

小于

...字段内容小于指定的表达式。

>=

大于或等于

...该字段内容大于或等于指定表达式。

<=

小于或等于

...该字段内容小于或等于指定的表达式。


LibreOffice 命令

SQL 命令

含义

如果...,则条件满足

IS EMPTY

IS NULL

为空

... the field contains no data. For Yes/No fields with three possible states, this command automatically queries the undetermined state (neither Yes nor No).

IS NOT EMPTY

IS NOT NULL

非空

... the field is not empty, i.e it contains data.

LIKE

placeholder (*) for any number of characters

placeholder (?) for exactly one character

LIKE

placeholder (%) for any number of characters

Placeholder (_) for exactly one character

是一个元素,位于

... the data field must contain the indicated search string in the order given. The position of the (*) placeholder indicates whether the searched for string (x) occurs before the placeholder (x*), after the placeholder (*x) or between the placeholders (*x*). Instead of the familiar file system (*) placeholder, you can also use the SQL (%) character as a placeholder in SQL queries in the LibreOffice interface.

The (*) or (%) placeholder stands for any number of characters. The question mark (?) in the LibreOffice interface or the underscore (_) in SQL queries is used to represent exactly one character.

NOT LIKE

NOT LIKE

不是一个元素,位于

... the field does not contain data having the specified expression.

BETWEEN x AND y

BETWEEN x AND y

位于区间 [x,y] 中

... the field contains a data value that lies between the two values x and y.

NOT BETWEEN x AND y

NOT BETWEEN x AND y

不在区间 [x,y] 中

... the field contains a data value that does not lie between the two values x and y.

IN (a; b; c...)

Note that semicolons are used as separators in all value lists!

IN (a, b, c...)

含有 a, b, c...

... the field name contains one of the specified expressions a, b, c,... Any number of expressions can be specified, and the result of the query is determined by a boolean OR operator. The expressions a, b, c... can be either numbers or characters

NOT IN (a; b; c...)

NOT IN (a, b, c...)

不包含 a, b, c...

... the field does not contain one of the specified expressions a, b, c,...

= TRUE

= TRUE

值为 True

...该字段名称值为 True。

= FALSE

= FALSE

有值 false

... the field data value is set to false.


示例

='Ms.'

返回带有字段内容 "Ms." 的字段。

<'2001-01-10'

返回2001年1月10日之前的日期

LIKE 'g?ve'

returns records with field content such as "give" and "gave".

LIKE 'S*'

returns records with field contents such as "Sun".

BETWEEN 10 AND 20

returns records with field content between the values 10 and 20. (The fields can be either text fields or number fields).

IN (1; 3; 5; 7)

returns records with the values 1, 3, 5, 7. If the field name contains an item number, for example, you can create a query that returns the item having the specified number.

NOT IN ('Smith')

returns records that do not contain "Smith".


LikeEscape Sequence:{escape 'escape-character'}

示例:select * from aticle where artname like 'The *%' {escape '*'}

该示例得到所有项目名称以 'The *' 开头的条目。这意味着你也可以查找其他要被解释为占位符的字符,如 *、?、_、% 或句号。

Outer Join换码顺序:{oj outer-join}

示例:select Artikel.* from {oj item LEFT OUTER JOIN orders ON item.no=orders.ANR}

查询文字字段

To query the content of a text field, you must put the expression between single quotes. The distinction between uppercase and lowercase letters depends on the database in use. LIKE, by definition, is case-sensitive (though some databases don't interpret this strictly).

查询日期字段

Date fields are represented as #Date# to clearly identify them as dates. Date, time and date/time constants (literals) used in conditions can be of either the SQL Escape Syntax type, or default SQL2 syntax.

Date Type Element

SQL Escape syntax #1 - may be obsolete

SQL Escape syntax #2

SQL2 syntax

日期

{D'YYYY-MM-DD'}

{d 'YYYY-MM-DD'}

'YYYY-MM-DD'

时间

{D'HH:MM:SS'}

{t 'HH:MI:SS[.SS]'}

'HH:MI:SS[.SS]'

日期时间

{D'YYYY-MM-DD HH:MM:SS'}

{ts 'YYYY-MM-DD HH:MI:SS[.SS]'}

'YYYY-MM-DD HH:MI:SS[.SS]'


示例:select {d '1999-12-31'} from world.years

Example: select * from mytable where years='1999-12-31'

All date expressions (date literals) must be enclosed with single quotation marks. (Consult the reference for the particular database and connector you are using for more details.)

查询是/否字段

要查询“是/否”字段,请在 dBASE 表中使用下列语法:

状态

查询条件

示例

对于 dBASE 表格:不等于任何给定的值

=1 返回所有“是/否”字段状态为“是”或“至”的记录(黑色标记)。

.

=0 返回所有“是/否”字段状态为“否”或“关闭”的记录(无选择)。

Null

IS NULL

IS NULL 返回所有是/否字段无“是”或“否”这两种状态的记录(灰色标记)。


批注图标

所用的语法取决于使用的数据库系统。同时还需要注意,“是/否”字段的定义可能会有所不同(仅有 2 种状态,而不是 3 种)。


参数查询

Parameter queries allow the user to input values at run-time. These values are used within the criteria for selecting the records to be displayed. Each such value has a parameter name associated with it, which is used to prompt the user when the query is run.

Parameter names are preceded by a colon in both the Design and SQL views of a query. This can be used wherever a value can appear. If the same value is to appear more than once in the query, the same parameter name is used.

In the simplest case, where the user enters a value which is matched for equality, the parameter name with its preceding colon is simply entered in the Criterion row. In SQL mode this should be typed as WHERE "Field" = :Parameter_name

警告图标

Parameter names may not contain any of the characters <space>`!"$%^*()+={}[]@'~#<>?/,. They may not be the same as field names or SQL reserved words. They may be the same as aliases.


提示图标

A useful construction for selecting records based on parts of a text field's content is to add a hidden column with "LIKE '%' || :Part_of_field || '%'" as the criterion. This will select records with an exact match. If a case-insensitive test is wanted, one solution is to use LOWER (Field_Name) as the field and LIKE LOWER ( '%' || :Part_of_field || '%' ) as the criterion. Note that the spaces in the criterion are important; if they are left out the SQL parser interprets the entire criterion as a string to be matched. In SQL mode this should be typed as LOWER ( "Field_Name" ) LIKE LOWER ( '%' || :Part_of_field || '%' ).


Parameter queries may be used as the data source for subforms, to allow the user to restrict the displayed records.

输入参数值

The Parameter Input dialog asks the user to enter the parameter values. Enter a value for each query parameter and confirm by clicking OK or typing Enter.

The values entered by the user may consist of any characters which are allowable for the SQL for the relevant criterion; this may depend on the underlying database system.

提示图标

The user can use the SQL wild-card characters "%" (arbitrary string) or "_" (arbitrary single character) as part of the value to retrieve records with more complex criteria.


SQL 模式

SQL 意为 "Structured Query Language"(结构化查询语言),描述用于查询、更新和管理关系数据库的指令。

In LibreOffice you do not need any knowledge of SQL for most queries, since you do not have to enter the SQL code. If you create a query in the query designer, LibreOffice automatically converts your instructions into the corresponding SQL syntax. If, with the help of the Switch Design View On/Off button, you change to the SQL view, you can see the SQL commands for a query that has already been created.

You can formulate your query directly in SQL code. Note, however, that the special syntax is dependent upon the database system that you use.

If you enter the SQL code manually, you can create SQL-specific queries that are not supported by the graphical interface in the Query designer. These queries must be executed in native SQL mode.

By clicking the Run SQL command directly icon in the SQL view, you can formulate a query that is not processed by LibreOffice and sent directly to the database engine.