Skip to main content

Combine the AND and OR Conditions

Last updated 8/04/2022

Combine the AND and OR Conditions in Five

Criteria can be a combination of AND and OR clauses. Combining the two enables you to perform complex filtering. AND conditions will return rows matching all the specified conditions, while OR conditions will perform the opposite and only retrieve rows that match either conditions.


Example
WHERE Condition_1
AND; Condition_2
...
OR; Condition_4

Create a Query with an AND and OR Condition

  1. Select Menus in the menu.

  2. Click the Add Item button in the list app bar.

  3. Type a title in the Title field.

  4. Click in the Query field to open the Table and Fields Designer.


Add a query
Figure 1 - Add a query

  1. Click the Add Table button.

Add Table Button
Figure 2 - Add Table button

  1. Select a table.

Select Table Name
Figure 3 - Select table name

  1. Click outside of the Table Selector dialog box to close the window.

  2. Select the table and drag to the desired position on the Designer.

  3. Double click the field names required for the query.

  4. In field one, type a condition in the Criteria field.

  5. In field two, type a condition in the Criteria field.

  6. Click the Add Or button.


Add an AND Condition
Figure 4 - Add an AND condition

  1. In the new row, type an OR condition in a Criteria field.

AND OR Condition
Figure 5 - Add an OR condition


Criteria Examples

> 1000

= 'Brisbane'

= 'Aspley'



info

This AND OR example will return all patients in the Brisbane suburb that have a patient number greater than 1000, or, all patients in the Aspley suburb.



Result Set
Figure 6 - Result set


tip

The AND and OR conditions can be set for any field that makes logical sense in the Query Builder.


  1. Click the Save button in the Table and Fields Designer app bar.

Save the query
Figure 7 - Save the query

  1. Click the Fields tab.

Fields Tab
Figure 8 - Fields tab

  1. Click the Save button in the form app bar.

Save the form
Figure 9 - Save the form


danger

Ensure to click the Fields tab to have all your query fields saved before committing the form.


Add an AND and OR Condition with Multiple Tables

  1. Select Menus in the menu.

  2. Click the Add Item button in the list app bar.

  3. Type a title in the Title field.

  4. Click in the Query field to open the Table and Fields Designer.


Add a query
Figure 10 - Add a query

  1. Click the Add Table button.

Add Table Button
Figure 11 - Add Table button

  1. Select a table.

  2. Select table two.


Select Table Names
Figure 12 - Select table names

  1. Click outside the Table Selector dialog box to close the window.

  2. Select each table and drag to the desired position on the Designer.

  3. In table one, double click the field names required for the query.

  4. In table two, double click the field names required for the query.

  5. In table one, type a condition in the Criteria field for a field.


Select fields and criteria
Figure 13 - Select fields and criteria


Criteria Example

< 1000



info

This WHERE statement will return all patients that have a patient number less than 1000. Because the first name, last name, suburb, date, time, and details fields are included in the query, these are also included in the result set.



Result Set
Figure 14 - Result set

  1. In table two, type a condition in the Criteria field for a field.

  2. Click the Add Or button.


Add an AND Condition across Multiple Tables
Figure 15 - Add an AND condition across multiple tables


Criteria Examples

< 1000

> '12:00PM'



info

This AND statement will return all patients that have a patient number less than 1000 and an appointment after 12:00 PM. Because the first name, last name, suburb, date, and details fields are included in the query, these are also included in the result set.

Five will automatically create the JOIN between these two tables.



Result Set
Figure 16 - Result set

You will notice Five has prefixed all fields with their table name, for example, Patient.Details. This is required to eliminate any ambiguity as to which field is being referenced, as the same field name may exist in both of the tables.

  1. In the new row, type an OR condition in a Criteria field.

Add an OR Condition
Figure 17 - Add an OR condition


Criteria Examples

< 1000

> '12:00 PM'

= 'Brisbne'



info

This AND OR statement will return all patients that have a patient number less than 1000 and have an appointment after 12:00 PM or in the Brisbane suburb. Because the first name, last name, date, and details fields are included in the query, these are also included in the result set.



Result Set
Figure 18 - Result set


tip

You can create as many AND and OR combinations across multiple tables that are required for your result set.



  1. Click the Save button in the Table and Fields Designer app bar.

Save the query
Figure 19 - Save the query

  1. Click the Fields tab.

Fields Tab
Figure 20 - Fields tab

  1. Click the Save button in the form app bar.

Save the form
Figure 21 - Save the form