{{SELECT}} statement syntax:
SELECT
[ALL | DISTINCT]
, ...
[FROM
[WHERE ]
[GROUP BY
[ASC | DESC], ... ]
[HAVING ]
[ORDER BY
[ASC | DESC], ...]
[LIMIT OFFSET }]
The {{SELECT}} statement is used for the retrieval of data from one or more components.
* Each {{select_expr}} indicates a column or an aggregate function value that you want to retrieve. There must be at least one {{select_expr}} in every {{SELECT}} statement.
SELECT a.name, a.amount FROM Account a
* An asterisk can be used for selecting all columns from given component. Even when using an asterisk the executed sql queries never actually use it (Doctrine converts asterisk to appropriate column names, hence leading to better performance on some databases).
SELECT a.* FROM Account a
* {{FROM}} clause {{components}} indicates the component or components from which to retrieve records.
SELECT a.* FROM Account a
SELECT u.*, p.*, g.* FROM User u LEFT JOIN u.Phonenumber p LEFT JOIN u.Group g
* The {{WHERE}} clause, if given, indicates the condition or conditions that the records must satisfy to be selected. {{where_condition}} is an expression that evaluates to true for each row to be selected. The statement selects all rows if there is no {{WHERE}} clause.
SELECT a.* FROM Account a WHERE a.amount > 2000
* In the {{WHERE}} clause, you can use any of the functions and operators that DQL supports, except for aggregate (summary) functions
* The {{HAVING}} clause can be used for narrowing the results with aggregate functions
SELECT u.* FROM User u LEFT JOIN u.Phonenumber p HAVING COUNT(p.id) > 3
* The {{ORDER BY}} clause can be used for sorting the results
SELECT u.* FROM User u ORDER BY u.name
* The {{LIMIT}} and {{OFFSET}} clauses can be used for efficiently limiting the number of records to a given {{row_count}}
SELECT u.* FROM User u LIMIT 20
+++ DISTINCT keyword
+++ Aggregate values
Aggregate value {{SELECT}} syntax:
// SELECT u.*, COUNT(p.id) num_posts FROM User u, u.Posts p WHERE u.id = 1 GROUP BY u.id
$query = new Doctrine_Query();
$query->select('u.*, COUNT(p.id) num_posts')
->from('User u, u.Posts p')
->where('u.id = ?', 1)
->groupby('u.id');
$users = $query->execute();
echo $users->Posts[0]->num_posts . ' posts found';