1
0
mirror of synced 2024-12-15 15:46:02 +03:00
doctrine2/manual/docs/en/dql-doctrine-query-language/select-queries.txt
2007-10-17 21:16:49 +00:00

87 lines
2.6 KiB
Plaintext

{{SELECT}} statement syntax:
<code type="sql">
SELECT
[ALL | DISTINCT]
<select_expr>, ...
[FROM <components>
[WHERE <where_condition>]
[GROUP BY <groupby_expr>
[ASC | DESC], ... ]
[HAVING <where_condition>]
[ORDER BY <orderby_expr>
[ASC | DESC], ...]
[LIMIT <row_count> OFFSET <offset>}]
</code>
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.
<code type="sql">
SELECT a.name, a.amount FROM Account a
</code>
* 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).
<code type="sql">
SELECT a.* FROM Account a
</code>
* {{FROM}} clause {{components}} indicates the component or components from which to retrieve records.
<code type="sql">
SELECT a.* FROM Account a
SELECT u.*, p.*, g.* FROM User u LEFT JOIN u.Phonenumber p LEFT JOIN u.Group g
</code>
* 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.
<code type="sql">
SELECT a.* FROM Account a WHERE a.amount > 2000
</code>
* 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
<code type="sql">
SELECT u.* FROM User u LEFT JOIN u.Phonenumber p HAVING COUNT(p.id) > 3
</code>
* The {{ORDER BY}} clause can be used for sorting the results
<code type="sql">
SELECT u.* FROM User u ORDER BY u.name
</code>
* The {{LIMIT}} and {{OFFSET}} clauses can be used for efficiently limiting the number of records to a given {{row_count}}
<code type="sql">
SELECT u.* FROM User u LIMIT 20
</code>
+++ DISTINCT keyword
+++ Aggregate values
Aggregate value {{SELECT}} syntax:
<code type="php">
// 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';
</code>