6.2.8 Aliases
Aliases are Snooze’s mechanism for letting you to refer to individual instances of an entity or expression within a query.
You can introduce aliases for entities, attributes or expressions using the following forms (see below for examples):
Introduces an alias for expr using the same scoping
rules as define.
Like define-alias but uses the same scoping rules as
let.
6.2.8.1 Entity aliases
It is sometimes necessary to refer to more than one instance of an entity within a query. Snooze lets you do this by defining aliases for entities. For example:
(define-alias father person) |
; Luke's father: |
(find-one |
(sql (select #:what father |
#:from (inner person |
father |
(= person.father-id father.id)) |
#:where (= person.name "Luke")))) |
6.2.8.2 Attribute and expression aliases
It is sometimes useful to assign an alias to an attribute or expression.
For example, if you are #:ordering on an expression, SQL 97 requires you to add the same expression to your #:what clause. The only way to do this is to alias the expression:
; All people and BMIs, ordered by BMI: |
(let-alias ([bmi (/ person.weight person.height)]) |
(find-all |
(sql (select #:what (person bmi) |
#:from person |
#:order ((asc bmi)))))) |