Query Design

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

要访问此命令...

在数据库文件窗口中,点击「查询」图标,然后选择「编辑 - 编辑」。


note

大多数数据库使用查询对数据库表进行筛选或排序,以在计算机上显示记录。视图提供与查询相同的功能,但在服务器端。如果数据库位于支持视图的服务器上,则可以使用视图过滤服务器上的记录,以加快显示时间。


note

从数据库文档的选项卡页中选择创建视图命令,可以看到类似于此处描述的查询设计窗口的视图设计窗口。


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

设计视图

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

设计视图的下部窗格是定义查询的位置。要定义查询,请指定要包含的数据库字段名称以及显示字段的条件。要重新排列设计视图下窗格中的列,请将列标题拖动到新位置,或选择该列,然后下+方向键。

In the top of the query Design View window, the icons of the Query Design Bar and the Design bar are displayed.

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

“查询设计视图”中的键

函数

F4

预览

F5

执行查询

F7

添加表或查询


浏览

第一次打开查询设计时,您会看到一个对话框,其中必须首先选择将作为新查询基础的表或查询。

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

note

在设计查询的过程中,您无法修改选定的表。


移除表

要从设计视图中移除表格,请单击表格窗口的上边框并显示关联菜单。可以使用删除命令从设计视图中移除表格。另一个选项是按Delete键。

移动表格并修改表格大小

您可以根据自己的喜好调整表格的大小和排列方式。要移动表格,请将上边框拖动到所需位置。通过将鼠标光标定位在边框或角上并拖动表格直到达到所需大小,放大或缩小表格的显示大小。

表关系

如果一个表中的字段名与另一个表中的字段名之间存在数据关系,则可以将这些关系用于查询。

例如,如果您有一个由商品编号标识的商品电子表格,以及一个客户电子表格,其中记录了客户使用相应商品编号订购的所有商品,那么这两个“商品编号”数据字段之间存在关系。如果现在要创建一个查询,返回客户订购的所有商品,则必须从两个电子表格中检索数据。为此,您必须将两个电子表格中的数据之间存在的关系通知LibreOffice。

要执行此操作,请单击表中的字段名称(例如,Customer表中的字段名称“Item Number”),按住鼠标按钮,然后将字段名称拖动到另一个表的字段名称(“Item table中的Item Number”)。松开鼠标按钮时,会出现一条连接两个表窗口之间的两个字段的线。在生成的SQL查询中输入相应的条件,即两个字段名称的内容必须相同。

只有在使用LibreOffice作为关系数据库的接口时,才能创建基于多个相关工作表的查询。

note

不能在查询中访问来自不同数据库的表。涉及多个表的查询只能在一个数据库中创建。


指定关系类型

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显示关联菜单,并在其中选择命令编辑。有些数据库只支持可能的联接类型的子集。

删除关系

要删除两个表之间的关系,请单击连接线,然后按delete键。

或者,删除关系对话框中涉及的字段中的相应条目。或按Tab键,直到连接向量高亮显示,然后按Shift+F10打开关联菜单并选择删除命令。

定义查询

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

指定字段名称

首先,从要添加到查询的表中选择所有字段名。可以通过拖放或双击表窗口中的字段名来完成此操作。使用拖放方法,使用鼠标将字段名称从表窗口拖动到查询设计窗口的下部区域。执行此操作时,可以决定查询设计窗口中的哪一列将接收所选字段。也可以通过双击选择字段名称。然后,它将被添加到查询设计窗口中的下一个空闲列中。

删除字段名称

要从查询中删除字段名称,请单击字段的列标题,然后在关联菜单上为该列选择删除命令。

保存查询

使用标准工具栏上的保存图标保存查询。您将看到一个对话框,要求您输入查询的名称。如果数据库支持架构,还可以输入架构名称。

架构

输入分配给查询或表视图的模式名称。

查询名称或表视图名称

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

筛选数据

要筛选查询数据,请在查询设计窗口的下部区域中设置所需的条件。以下选项可用:

字段

输入查询中引用的数据字段的名称。过滤器选项行中的所有设置都引用此字段。如果您在此处用鼠标单击激活单元格,您将看到一个箭头按钮,可用于选择字段。“Table name.*”选项选择所有数据字段,其效果是指定的条件将应用于所有表字段。

别名

指定别名。查询中将列出此别名,而不是字段名。这样就可以使用用户定义的列标签。例如,如果数据字段名为PtNo,而不是该名称,您希望在查询中显示PartNum,请输入PartNum作为别名。

在SQL语句中,别名定义如下:

从表中选择列作为别名。

示例:


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

表格

此处列出了所选数据字段的相应数据库表。如果用鼠标单击激活此单元格,将显示一个箭头,允许您为当前查询选择其他表。

排序

如果单击此单元格,可以选择排序选项:升序、降序和未排序。文本字段将按字母顺序和数字字段进行排序。对于大多数数据库,管理员可以在数据库级别设置排序选项。

可见的

如果为数据字段标记可见属性,则该字段将在结果查询中可见显示。如果仅使用数据字段来表示条件或进行计算,则不一定需要显示它。

条件

指定筛选数据字段内容的第一个条件。

或者

在这里,您可以为每一行输入一个额外的过滤条件。单个列中的多个条件将被解释为布尔或。

您还可以使用“查询设计”窗口下部区域中行标题的关联菜单,根据以下函数插入过滤器:

函数

此处可用的功能取决于数据库引擎提供的功能。

如果使用嵌入式HSQL数据库,则函数行中的列表框提供以下选项:

选项

SQL

影响

无功能

不会执行任何功能。

平均值

AVG

计算字段的算术平均值。

计数

COUNT

确定表中的记录数。空字段可以参与计数(a)或被排除(b)。

a) COUNT(*): 传递星号作为参数对表中的所有记录进行计数。

b) COUNT (column):将字段名作为参数传递仅统计指定字段中包含值的记录。字段值为空(即不包含文本或数字值)的记录将不被统计。

最大值

MAX

确定该字段记录的最大值。

最小值

MIN

确定该字段记录的最小值。

求和

SUM

计算关联字段的记录值之和。

分组

GROUP BY

根据所选字段名称对查询数据分组。根据指定的组执行函数。在SQL中,此选项对应于GROUP BY子句。如果添加了条件,则此条目将出现在SQL HAVING子子句中。


您还可以直接在SQL语句中输入函数调用。语法为:

SELECT FUNCTION(column) FROM table.

例如,SQL中用于求和的函数调用为:


SELECT SUM("Price") FROM "Article".

除了组合函数外,上述函数称为聚合函数。这些函数用于计算数据以根据结果创建汇总数。也可以使用列表框中未列出的其他函数。这取决于使用中的特定数据库引擎以及用于连接到该数据库引擎的基本驱动程序提供的当前功能。

要使用列表框中未列出的其他功能,必须在字段下手动输入。

还可以为函数调用指定别名。如果不想在列标题中显示查询字符串,请在别名下输入所需的替换名称。

SQL语句中对应的函数是:

SELECT FUNCTION() AS alias FROM table

示例:


SELECT COUNT(*) AS count FROM "Item"
note

如果运行这样的函数,则除了作为“Group”函数中的参数外,不能为查询插入任何其他列。


示例

在下列示例中,在两个表之间执行了一个查询:表 "Item" 及其 "Item_No" 字段,以及表 "Suppliers" 及其 "Supplier_Name" 字段。另外,两个表中都存在一个共同的字段名:"Supplier_No"。

要创建包含交付三个以上项目的所有供应商的查询,需要执行以下步骤。

  1. 在查询设计中插入“Item”和“Suppliers”表。

  2. 如果还没有此类关系,请链接两个表的“Supplier_No”字段。

  3. 双击“Item”表中的“Item_No”字段。使用关联菜单显示函数行,然后选择“计数”函数。

  4. 输入>3作为条件,并禁用“可见的”字段。

  5. 双击“Suppliers”表中的“Supplier_Name”字段,选择“组合”函数。

  6. 运行查询。

如果“Item”表中存在“price”(针对物品的个别价格)和“Supplier_No”(针对物品的供应商)字段,则可以通过以下查询获得供应商提供的物品的平均价格:

  1. 在查询设计中插入“Item”表。

  2. 双击“价格”和“供应商编号”字段。

  3. 启用函数行,并从“价格”字段中选择平均值函数。

  4. 您还可以在别名行中输入“Average”(平均值)(不带引号)。

  5. 为“Supplier_No”字段选择“组合”。

  6. 运行查询。

The following context menu commands and symbols are available:

Functions

Shows or hides a row for the selection of functions.

Table Name

Shows or hides the row for the table name.

Alias Name

Shows or hides the row for the alias name.

Distinct Values

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).

For example, if the name "Smith" occurs several times in your address database, you can choose the Distinct Values command to specify in the query that the name "Smith" will occur only once.

For a query involving several fields, the combination of values from all fields must be unique so that the result can be formed from a specific record. For example, you have "Smith in Chicago" once in your address book and "Smith in London" twice. With the Distinct Values command, the query will use the two fields "last name" and "city" and return the query result "Smith in Chicago" once and "Smith in London" once.

In SQL, this command corresponds to the DISTINCT predicate.

Limit

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.

Formulating filter conditions

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:

Operator

Meaning

Condition is satisfied if...

=

equal to

... the content of the field is identical to the indicated expression.

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

<>

not equal to

... the content of the field does not correspond to the specified expression.

>

greater than

... the content of the field is greater than the specified expression.

<

less than

... the content of the field is less than the specified expression.

>=

greater than or equal to

... the content of the field is greater than or equal to the specified expression.

<=

less than or equal to

... the content of the field is less than or equal to the specified expression.


LibreOffice command

SQL command

Meaning

Condition is satisfied if...

IS EMPTY

IS NULL

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

is not empty

... 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

is an element of

... the data field contains the indicated expression. The (*) placeholder indicates whether the expression x occurs at the beginning of (x*), at the end of (*x) or inside the field content (*x*). You can enter as a placeholder in SQL queries either the SQL % character or the familiar (*) file system placeholder 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

Is not an element of

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

BETWEEN x AND y

BETWEEN x AND y

falls within the interval [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

Does not fall within the interval [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...)

contains 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...)

does not contain a, b, c...

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

= TRUE

= TRUE

has the value True

... the field name has the value True.

= FALSE

= FALSE

has the value false

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


Examples

='Ms.'

returns field names with the field content "Ms."

<'2001-01-10'

returns dates that occurred before January 10, 2001

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".


Like Escape Sequence: {escape 'escape-character'}

示例:


SELECT * FROM Item WHERE ItemName LIKE 'The *%' {escape '*'}

The example will give you all of the entries where the item name begins with 'The *'. This means that you can also search for characters that would otherwise be interpreted as placeholders, such as *, ?, _, % or the period.

Outer Join Escape Sequence: {oj outer-join}

示例:


SELECT Article.* FROM {oj item LEFT OUTER JOIN orders ON item.no=orders.ANR}

Querying text fields

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).

Querying date fields

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

Date

{D'YYYY-MM-DD'}

{d 'YYYY-MM-DD'}

'YYYY-MM-DD'

Time

{D'HH:MM:SS'}

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

'HH:MI:SS[.SS]'

DateTime

{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

示例:


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.)

Querying Yes/No fields

To query Yes/No fields, use the following syntax for dBASE tables:

Status

Query criterion

Example

Yes

for dBASE tables: not equal to any given value

=1 returns all records where the Yes/No field has the status "Yes" or "On" (selected in black),

No

.

=0 returns all records for which the Yes/No field has the status "No" or "Off" (no selection).

Null

IS NULL

IS NULL returns all records for which the Yes/No field has neither of the states Yes or No (selected in gray).


note

The syntax depends on the database system used. You should also note that Yes/No fields can be defined differently (only 2 states instead of 3).


Parameter queries

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

warning

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.


tip

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.

Parameter Input

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.

tip

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 Mode

SQL stands for "Structured Query Language" and describes instructions for updating and administering relational databases.

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.

请支持我们!