4D v16.3QUERY BY ATTRIBUTE |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
4D v16.3
QUERY BY ATTRIBUTE
QUERY BY ATTRIBUTE
QUERY BY ATTRIBUTE looks for records matching the query string defined using the objectField, attributePath, queryOp and value parameters, and returns a selection of records for aTable. Note: For more information on Object fields (new in 4D v15), please refer to the section of the Design Reference manual. QUERY BY ATTRIBUTE changes the current selection of aTable for the current process and makes the first record of the new selection the current record. If the aTable parameter is omitted, the command applies to the default table. If no default table has been set, an error occurs. The optional conjOp parameter is used to join QUERY BY ATTRIBUTE calls when defining multiple queries. The conjunction operators available are the same as the ones for the QUERY command:
The conjOp parameter is not used for the first QUERY BY ATTRIBUTE call of a multiple query, or if the query is a simple query. If you omit it within a multiple query, the AND (&) operator is used by default. In objectField, pass the Object field whose attribute(s) you want to query. If it belongs to a One table related to aTable with an automatic or manual relation, the objectField may belong to another table. In attributePath, pass the path of the attribute whose values you want to compare for each record, for example "children.girls.age". If you pass a single name, for example "place", you designate the corresponding attribute found at the first level of the object field. Notes:
The queryOp parameter is the comparison operator that is applied between objectField and value. You can pass one of the symbols shown here:
Note: It is also possible to specify the comparison operator as an text expression instead of a symbol. See the QUERY command description for more information. value is the data against which attributePath will be compared. The value can be any expression that evaluates to the same data type as attributePath. The value is evaluated once, at the beginning of the query. The value is not evaluated for each record. To query for a string contained within a string (a "contains" query), use the wildcard symbol (@) in value to isolate the string to be searched for as shown in this example: "@Smith@". Note that in this case, the search only partially benefits from the index (compactness of data storage). Here is the structure of a query by attribute: QUERY BY ATTRIBUTE([Table] ;[Table]ObjectField ;"attribute1.attribute2";=;value) Note: An implicit criteria for all operators (except #) is that the Object field contains an attribute. However, for the # operator, it can be undefined (see below). Queries by attribute using the "#" operator can have different results depending on whether or not the Map NULL values to blank values property is checked for the object field:
Here are the rules for building multiple queries by attribute:
Note: Each table maintains its own currently-built query. This means that you can create multiple queries simultaneously, one for each table. No matter which way a query has been defined:
Dates are stored in objects according to database settings; by default, the time zone is taken into account (see the JSON use local time selector in the SET DATABASE PARAMETER command).
!1973-05-22! -> "1973-05-21T23:00:00.000Z" This setting is also taken into account during queries, so you do not have to worry about it if you always use your database at the same place and if settings are the same on all machines that access the data. In this case, the following query will correctly return records whose Birthday attribute equals !1973-05-22! (saved as "1973-05-21T23:00:00.00Z"): QUERY BY ATTRIBUTE([Persons];[Persons]OB_Info;"Birthday";=;!1973-05-22!) If you do not want to use the GMT settings, you can modify these settings using the following instruction: SET DATABASE PARAMETER(JSON use local time;0) Keep in mind that the scope of this setting is the process only. If you execute this instruction, then October 1st, 1965 will be stored "1965-10-01T00:00:00.000Z" but you will need to set the same parameter before launching your queries: SET DATABASE PARAMETER(JSON use local time;0) You can use the virtual "length" property with this command. This property is available automatically for all array type attributes and returns the size of the array, i.e. the number of elements it contains. It can be used in the context of executing the QUERY BY ATTRIBUTE command (see example 4). In this example, the "age" attribute is either a string or an integer and we want to find people whose age is between 20 and 29. The first two lines query the attribute as an integer (>=20 and < 30) and the last ones query the field as a string (starts with "2" but is different from "2".) QUERY BY ATTRIBUTE([Persons];[Persons]OB_Info;"age";>=;20;*) The QUERY BY ATTRIBUTE command can be used to find records where certain attributes are defined (or are not defined). To do this, you have to use an empty object. //Find records where e-mail is defined in the object field //Find records where zip code is NOT defined in the object field Note: This specific syntax is not supported with array type attributes. Searching for NULL values in array elements will give invalid results. You want to search a field containing array attributes. With the following two records: Record1: Record2: ... QUERY BY ATTRIBUTE will find people with a location in "paris" using this statement: //flag the array attribute with "[]" syntax Note: If you defined several criteria on the same array attribute, the matched criteria will not necessarily apply to the same array element. In the following example, the query returns "smith" because it has a "locations" element whose "kind" is "home" and a "locations" element whose "city" is "paris", even if it's not the same element: QUERY BY ATTRIBUTE([People];[People]OB_Field;"locations[].kind";=;"home";*) This example illustrates the use of the virtual "length" property. Your database has a [Customer]full_Data object field with the following data: You want to get the records for any customers who have two or more children. To do this, you can write: QUERY BY ATTRIBUTE([Customer];[Customer]full_Data;"Children.length";>=;2) If the query is carried out correctly, the OK system variable is set to 1.
See also
QUERY SELECTION BY ATTRIBUTE
|
PROPERTIES
Product: 4D HISTORY
Created: 4D v15 ARTICLE USAGE
4D Language Reference ( 4D v16) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||