Query Design/text/shared/explorer/database/02010100.xhpviews; creating database views (Base)queries; creating in design view (Base)designing; queries (Base)design view; queries/views (Base)joining;tables (Base)tables in databases; joining for queries (Base)queries; joining tables (Base)tables in databases; relations (Base)relations; joining tables (Base)queries; deleting table links (Base)criteria of query design (Base)queries; formulating filter conditions (Base)filter conditions;in queries (Base)parameters; queries (Base)queries; parameter queries (Base)SQL; queries (Base)native SQL (Base)mw added "(Base)" to all entriesQuery DesignThe Query Design View allows you to create and edit a database query.Most databases use queries to filter or to sort database tables to display records on your computer. Views offer the same functionality as queries, but on the server side. If your database is on a server that supports views, you can use views to filter the records on the server to speed up the display time.Selecting the Create View command from the Tables tab page of a database document, you see the View Design window that resembles the Query Design window described here.The Query Design window layout is stored with a created query, but cannot be stored with a created view.The Design ViewTo create a query, click the Queries icon in a database document, then click Create Query in Design View.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 CommandCtrl+arrow key.In the top of the query Design View window, the icons of the Query Design Bar and the Design bar are displayed.If you want to test a query, double-click the query name in the database document. The query result is displayed in a table similar to the Data Source View. Note: the table displayed is only temporary.Keys in Query Design View
KeyFunctionF4PreviewF5Run QueryF7Add Table or Query
BrowseWhen you open the query design for the first time, in order to create a new query, you can click Add Tables. You then see a dialog in which you must first select the table that will be the basis for the query.Double-click fields to add them to the query. Drag-and-drop to define relations.While designing a query, you cannot modify the selected tables.Remove tablesTo remove the table from Design View, click the upper border of the table window and display the context menu. You can use the Delete command to remove the table from the Design View. Another option is to press the Delete key.Move table and modify table sizeYou can resize and arrange the tables according to your preferences. To move tables, drag the upper border to the desired position. Enlarge or reduce the size in which the table is displayed by positioning the mouse cursor on a border or on a corner and dragging the table until it is the desired size.Table RelationsIf there are data relations between a field name in one table and a field name in another table, you can use these relations for your 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 tell $[officename] what the relationship 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 in the two windows appears. The corresponding condition that the content of the two field names must be identical is entered in the resulting SQL query.The creation of a query that is based on several related sheets is only possible if you use $[officename] as the interface for a relational database.You cannot access tables from different databases in a query. Queries involving multiple tables can only be created within one database.Specifying link typeIf you double-click the line connecting two linked fields or call the menu command Insert - New Relation, you can specify the type of link in the Relations dialog.Edit Join Properties. Alternatively, press Tab until the line is selected, then press Shift+F10 to display the context menu and there choose the command Edit. Some databases support only a subset of the possible join types.Deleting relationsTo delete a relation between two tables, click the connection line and then press the Delete key.Alternatively, delete the respective entries in Fields involved in the Relations dialog. Or press Tab until the connecting vector is displayed highlighted, then press Shift+F10 to open the context menu and select Delete command.Define querySelect conditions to define the query. Each column of the design table accepts a data field for the query. The conditions in one row are linked with a Boolean AND.Specify field nameFirst, select all field names from the tables that you want to add to the query. You can do this either with 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. As you do this, you can decide which column you want to add the field to. Select a field name by double-clicking. It will then be added to the next free column.Deleting field namesTo remove a field name from the query, click the column header of the field and choose the Delete command on the context menu for the column.Save queryUse the Save icon on the Standard Bar to save the query. You see a dialog that asks you to enter a name for the query. If the database supports schemas, you can also enter a schema.SchemaEnter the name of the schema that is assigned to the query or table view.Query name or table view nameEnter the name of the query or table view.Filtering dataTo filter data for the query, set the desired preferences in the lower area of the Design View. The following lines are available:FieldEnter the name of the data field that you referred to in the Query. All settings made in the lower rows refer to this field. If you activate a cell 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 and the criteria is valid for all table fields.AliasSpecifies an alias. This alias will be listed in a query instead of the field name. This makes it possible to use user-defined column labels. For example, if the data field has the name PtNo and, instead of that name, you would like to have PartNum appear in the query, enter PartNum as alias.In an SQL statement, aliases are defined as following:SELECT column AS alias FROM table.For example:SELECT "PtNo" AS "PartNum" FROM "Parts"TableThe corresponding database table of the selected data field is listed here. If you activate the a cell with a mouse click, an arrow will appear which enables you to select another table of the current query.SortIf you click the cell, you can select among the sorting options: ascending, descending and not sorted. Text fields will be sorted alphabetically and numerical fields numerically. For most databases, administrators can set the sorting options.VisibleIf you mark the Visible property for a data field, that field will be visible in the query. If you only use a data field to formulate a condition, you do not necessarily need to show it.CriteriaSpecifies the criteria by which the content of the data field should be filtered.orHere you can enter one additional criterion for filtering in each line. Multiple criteria in one column will be connected by an OR link.You can also use the context menu of the line headers in the lower area of the query design to insert another line for functions:FunctionsSelect a function to run in the query here. The functions you can run here depend on the database.If you are working with the HSQL database, the list box in the Function row offers you the following options:
OptionSQLEffectNo functionNo function will be executed.AverageAVGCalculates the arithmetic mean of a field.CountCOUNTDetermines the number of records in the table. Empty fields can either be counted (a) or not (b).a) COUNT(*): Passing an asterisk as the argument counts all records in the table.b) COUNT(column): Passing a field name as an argument counts only fields in which the field name in question contains a value. Null values (empty fields) will not be counted.MaximumMAXDetermines the highest value of a field.MinimumMINDetermines the lowest value of a field.SumSUMCalculates the sum of values of associated fields.GroupGROUP BYGroups query data according to the field name selected. 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.
You can also enter function calls directly into the SQL statement. The syntax is:SELECT FUNCTION(column) FROM table.For example, the function call in SQL for calculating a sum is:SELECT SUM("Price") FROM "Article".Except for the Group function, the above functions are so-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 system in use and on the current state of the Base driver.To use other functions not listed in the list box, you must enter them under Field.You can also assign aliases to function calls. If the query is not to be displayed in the column header, enter the desired name under Alias.The corresponding function in an SQL statement is:SELECT FUNCTION() AS alias FROM tableExample:SELECT COUNT(*) AS count FROM "Item"If you run this function, you cannot insert any additional columns for the query other than receiving these columns as a "Group" function.ExamplesIn the following example, a query is run through two tables: an "Item" table with the "Item_No" field and a "Suppliers" table with the "Supplier_Name" field. In addition, both tables have a common field name "Supplier_No."The following steps are required to create a query containing all suppliers who deliver more than three items.Insert the "Item" and "Suppliers" tables into the query design.Link the "Supplier_No" fields of the two tables if there is not already a relation of this type.Double-click the "Item_No" field from the "Item" table. Display the Function line using the context menu and select the Count function.Enter >3 as a criterion and disable the Visible field.Double-click the "Supplier_Name" field in the "Suppliers" table and choose the Group function.Run the query.If the "price" (for the individual price of an article) and "Supplier_No" (for the supplier of the article) fields exist in the "Item" table, you can obtain the average price of the item that a supplier provides with the following query:Insert the "Item" table into the query design.Double-click the "Price" and "Supplier_No" fields.Enable the Function line and select the Average function from the "Price" field.You can also enter "Average" in the line for the alias name (without quotation marks).Choose Group for the "Supplier_No" field.Run the query.The following context menu commands and symbols are available:FunctionsShows or hides a row for selection of functions.Table NameShows or hides the row for the table name.Alias NameShows or hides the row for the alias name.Distinct ValuesApplies only distinct values to the query. This applies to records containing data that appears several times in the selected fields. If the Distinct Values command is active, you will see only 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.Formulating filter conditionsWhen 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 $[officename] syntax, $[officename] automatically converts these into the corresponding SQL syntax. You can also enter the SQL command directly. The following tables give an overview of the operators and commands:
OperatorMeaningCondition 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 = will be automatically adopted.<>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.
$[officename] commandSQL commandMeaningCondition is satisfied if...IS EMPTYIS NULLis null... The field name is empty. For Yes/No fields with three states, this command automatically queries the undetermined state (neither Yes nor No).IS NOT EMPTYIS NOT NULLis not empty... the field name is not empty.LIKE(placeholder * for any number of charactersplaceholder ? for exactly one character)LIKEplaceholders; in SQL queries(% placeholder for any number of charactersPlaceholder _ 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 $[officename] interface.The * or % placeholder stands for any number of characters. The question mark (?) in the $[officename] interface or the underscore (_) in SQL queries is used to represent exactly one character.NOT LIKENOT LIKEIs not an element of... the field name does not contain the specified expression.BETWEEN x AND yBETWEEN x AND yfalls within the interval [x,y]... the field name contains a value that lies between the two values x and y.NOT BETWEEN x AND yNOT BETWEEN x AND yDoes not fall within the interval [x,y]... the field name contains a value that does not lie between the two values x and y.IN (a; b; c...)Note that the 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 an Or link. 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 name does not contain one of the specified expressions a, b, c,...= TRUE= TRUEhas the value True... the field name has the value True.= FALSE= FALSEhas the value false... the field name has the value 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 field names with field content such as "give" and "gave".LIKE 'S*'returns data fields with field contents such as "Sun".BETWEEN 10 AND 20returns field names 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 field names 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 field names that do not contain "Smith".
Like Escape Sequence: {escape 'escape-character'}Example: 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}Example: select Article.* from {oj item LEFT OUTER JOIN orders ON item.no=orders.ANR}Querying text fieldsTo 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 see it that strict).Querying date fieldsDate 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 ElementSQL Escape syntax #1 - may be obsoleteSQL Escape syntax #2SQL2 syntaxDate{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]'
Example: select {d '1999-12-31'} from world.yearsExample: select * from mytable where years='1999-12-31'All date expressions (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 fieldsTo query Yes/No fields, use the following syntax for dBASE tables:
StatusQuery criterionExampleYesfor 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).NullIS NULLIS NULL returns all records for which the Yes/No field has neither of the states Yes or No (selected in gray).
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 queriesYou must place the variable between square brackets (=[x]) to create a query with variable parameters. Alternatively, you can use an equal sign followed by a colon (=:x). When the query is executed, the program will display a dialog asking you for the expression to which the variable x should be assigned.If you query several parameters at the same time, you will see a list field in the dialog containing all of the parameters and an input line alongside each one. Enter the values, preferably from top to bottom, and press the Enter key after each line.Parameter queries with placeholders (*, _) or special characters (for example, ?) are not possible.If you formulate a parameter query and you save it with the variables, you can later create a query in which only the variables have to be replaced by the expressions that you want. $[officename] asks for these variables in a dialog as soon as you open the query.Parameter InputThe Parameter Input dialog asks you which variables you defined in the query. Enter a value for each query variable and confirm by clicking OK.Parameter queries are also used for subforms, since they work exclusively with queries for which the values to be invoked are read internally from a variable.A parameter query can have the following form in an SQL statement:select * from 'addresses' where 'name' = :placeholderSQL ModeSQL stands for "Structured Query Language" and describes instructions for updating and administering relational databases.In $[officename] 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 design, $[officename] 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 been created previously.You can formulate your query directly in the 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 Query design. 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 $[officename].