查詢設計

[查詢設計檢視] 允許您建立及編輯資料庫查詢。

若要使用此指令...

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.

按兩下欄位可以將其新增到查詢中。拖放以定義關係。

評註圖示

在設計查詢時,您無法修改選取的表格。


移除表格

若要從設計檢視移除一張表格,請按一下表格視窗的上邊框,然後呼叫右鍵功能表。利用「刪除」指令可將表格從設計檢視中刪除。此外,您也可以按下 (Delete) 鍵。

移動表格和變更表格大小

您可以隨意排列表格並變更其大小。若要移動表格,則利用滑鼠抓住上邊框,然後拖曳表格到想要的位置。若要放大或縮小表格的顯示,則拖曳邊框或角。

表格間的關係

如果一張表格中的資料欄位與另一張表格中的資料欄位有資料上的關聯,那麼您可以將這些關係用於查詢。

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 組合鍵顯示右鍵功能表,並在此處選擇 [編輯] 指令。某些資料庫只支援可能連結類型的子集。

刪除關聯

如果您要刪除兩個表格之間的關聯,則按一下連結線,然後按下 (Delete) 鍵。

或者,刪除[關係]對話方塊的[使用欄位]中相應的條目。或按住 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 "商品 No." AS 商品編號 FROM "商品"

表格

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 the records in which the specified field contains a value. Records in which the field has a Null value (i.e. contains no textual or numeric value) 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("價格") FROM "商品"。

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 數目 FROM "商品"

評註圖示

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


範例

下列範例中,將執行關於兩個表格的查詢:一個內含「商品_No」欄位的「商品」表和一個內含「供應商_名稱」欄位的「供應商」表格。此外,這兩個表格擁有一個共同欄位「供應商_No」。

若要建立包括所有供應超過三種商品以上的供應商的查詢,則須執行下列步驟:

  1. 將「商品」和「供應商」兩個表格插入查詢設計中。

  2. 連結兩個表格的「供應商_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. 按兩下「供應商」表格中的「供應商_名稱」欄位,然後選取「群組」功能。

  6. 執行查詢。

當「商品」表中出現「價格」(商品的單價) 和「供應商_No」(商品的供應商) 這兩個欄位時,您可以利用下列查詢某個供應商所提供商品的平均價格:

  1. 將「商品」表插入查詢設計中。

  2. 按兩下「價格」和「供應商_No」欄位。

  3. 啟動「功能」列,並於「價格」欄位中選擇「平均值」功能。

  4. 依喜好在列中填入代用名「平均值」(不含引號)。

  5. 在「供應商_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 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

不是組成部分

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

得出欄位內容為「Frau」的資料欄位。

<'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 商品 where 商品名稱 like 'The *%' {escape '*'}

此範例得出所有以「The *」開頭的商品名稱條目。因此您同樣可以搜尋解譯為萬用字元的字元,如 *、?、_、% 或 .。

Outer Join Escape Sequence: {oj outer-join}

範例:select 商品.* from {oj 商品 LEFT OUTER JOIN 訂購 ON 商品.No=訂購.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

範例: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 得出所有資料條目,其中「是/否」欄位擁有「否」或「不使用」狀況 (無標記)。

空缺

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.

Please support us!