Restricting the results of a simple query
Generally, you want to restrict the information
that queries return from a database instead of selecting all the information
that the database contains. With SQL, the WHERE clause lets you specify
Boolean expressions that data must satisfy to be included in the result.
For example, you submit the following SQL statement to a relational database.
SELECT * FROM addresses
WHERE city = "Chicago";
In response, the relational database returns
a result set with addresses only in the city of Chicago. In the preceding
SQL example, the Boolean expression applies the predicate = "Chicago" to the column city. The expression asserts that
addresses in the result set have “Chicago” as the city.
The following Gosu code constructs and executes a functionally equivalent query to the preceding SQL example.
uses gw.api.database.Query
var query = Query.make(Address)
query.compare(Address#City, Equals, "Chicago")
var select = query.select()
var result = select.iterator() // Execute the query and return an iterator to access the result.
In response to the preceding Gosu code, the application
database fetches all Address
instances from the application database that are in the city of Chicago.
